r385: 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.60 2001/01/12 21:53:27 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 = NULL;
1060     double *pYMag = NULL;
1061     if (rIF.isComplex()) {
1062       pYImag = new double [nx];
1063       pYMag = new double [nx];
1064     }
1065     for (int i = 0; i < nx; i++) {
1066       pX[i] = i;
1067       pYReal[i] = v[i][yCursor];
1068       if (rIF.isComplex()) {
1069         pYImag[i] = vImag[i][yCursor];
1070         pYMag[i] = ::sqrt (v[i][yCursor] * v[i][yCursor] + vImag[i][yCursor] * vImag[i][yCursor]);
1071       }
1072     }
1073     PlotFileDocument* pPlotDoc = dynamic_cast<PlotFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.plt", wxDOC_SILENT));
1074     if (! pPlotDoc) {
1075       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1076     } else {
1077       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1078       std::ostringstream os;
1079       os << "Row " << yCursor;
1080       std::string title("title ");
1081       title += os.str();
1082       rPlotFile.addEzsetCommand (title.c_str());
1083       rPlotFile.addEzsetCommand ("xlabel Column");
1084       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1085       rPlotFile.addEzsetCommand ("lxfrac 0");
1086       rPlotFile.addEzsetCommand ("box");
1087       rPlotFile.addEzsetCommand ("grid");
1088       rPlotFile.addEzsetCommand ("curve 1");
1089       rPlotFile.addEzsetCommand ("color 1");
1090       if (rIF.isComplex()) {
1091         rPlotFile.addEzsetCommand ("dash 1");
1092         rPlotFile.addEzsetCommand ("curve 2");
1093         rPlotFile.addEzsetCommand ("color 4");
1094         rPlotFile.addEzsetCommand ("dash 3");
1095         rPlotFile.addEzsetCommand ("curve 3");
1096         rPlotFile.addEzsetCommand ("color 0");
1097         rPlotFile.addEzsetCommand ("solid");
1098         rPlotFile.setCurveSize (4, nx);
1099       } else
1100         rPlotFile.setCurveSize (2, nx);
1101       rPlotFile.addColumn (0, pX);
1102       rPlotFile.addColumn (1, pYReal); 
1103       if (rIF.isComplex()) {
1104         rPlotFile.addColumn (2, pYImag);
1105         rPlotFile.addColumn (3, pYMag);
1106       }
1107       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1108         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1109       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1110       *theApp->getLog() << os.str().c_str() << "\n";
1111       rPlotFile.addDescription (os.str().c_str());
1112     }
1113     delete pX;
1114     delete pYReal;
1115     if (rIF.isComplex()) {
1116       delete pYImag;
1117       delete pYMag;
1118     }
1119     if (theApp->getSetModifyNewDocs())
1120       pPlotDoc->Modify(true);
1121     pPlotDoc->UpdateAllViews();
1122   }
1123 }
1124
1125 void
1126 ImageFileView::OnPlotCol (wxCommandEvent& event)
1127 {
1128   int xCursor, yCursor;
1129   if (! m_canvas->GetCurrentCursor (xCursor, yCursor)) {
1130     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1131     return;
1132   }
1133   
1134   const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
1135   ImageFileArrayConst v = rIF.getArray();
1136   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1137   int nx = rIF.nx();
1138   int ny = rIF.ny();
1139   
1140   if (v != NULL && xCursor < nx) {
1141     double* pX = new double [ny];
1142     double* pYReal = new double [ny];
1143     double* pYImag = NULL;
1144     double* pYMag = NULL;
1145     if (rIF.isComplex()) {
1146       pYImag = new double [ny];
1147       pYMag = new double [ny];
1148     }
1149     for (int i = 0; i < ny; i++) {
1150       pX[i] = i;
1151       pYReal[i] = v[xCursor][i];
1152       if (rIF.isComplex()) {
1153         pYImag[i] = vImag[xCursor][i];
1154         pYMag[i] = ::sqrt (v[xCursor][i] * v[xCursor][i] + vImag[xCursor][i] * vImag[xCursor][i]);
1155       }
1156     }
1157     PlotFileDocument* pPlotDoc = dynamic_cast<PlotFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.plt", wxDOC_SILENT));
1158     if (! pPlotDoc) {
1159       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1160     } else {
1161       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1162       std::ostringstream os;
1163       os << "Column " << xCursor;
1164       std::string title("title ");
1165       title += os.str();
1166       rPlotFile.addEzsetCommand (title.c_str());
1167       rPlotFile.addEzsetCommand ("xlabel Row");
1168       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1169       rPlotFile.addEzsetCommand ("lxfrac 0");
1170       rPlotFile.addEzsetCommand ("box");
1171       rPlotFile.addEzsetCommand ("grid");
1172       rPlotFile.addEzsetCommand ("curve 1");
1173       rPlotFile.addEzsetCommand ("color 1");
1174       if (rIF.isComplex()) {
1175         rPlotFile.addEzsetCommand ("dash 1");
1176         rPlotFile.addEzsetCommand ("curve 2");
1177         rPlotFile.addEzsetCommand ("color 4");
1178         rPlotFile.addEzsetCommand ("dash 3");
1179         rPlotFile.addEzsetCommand ("curve 3");
1180         rPlotFile.addEzsetCommand ("color 0");
1181         rPlotFile.addEzsetCommand ("solid");
1182         rPlotFile.setCurveSize (4, ny);
1183       } else
1184         rPlotFile.setCurveSize (2, ny);
1185       rPlotFile.addColumn (0, pX);
1186       rPlotFile.addColumn (1, pYReal); 
1187       if (rIF.isComplex()) {
1188         rPlotFile.addColumn (2, pYImag);
1189         rPlotFile.addColumn (3, pYMag);
1190       }
1191       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1192         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1193       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1194       *theApp->getLog() << os.str().c_str() << "\n";
1195       rPlotFile.addDescription (os.str().c_str());
1196     }
1197     delete pX;
1198     delete pYReal;
1199     if (rIF.isComplex()) {
1200       delete pYImag;
1201       delete pYMag;
1202     }
1203     if (theApp->getSetModifyNewDocs())
1204       pPlotDoc->Modify(true);
1205     pPlotDoc->UpdateAllViews();
1206   }
1207 }
1208
1209 #ifdef HAVE_FFT
1210 void
1211 ImageFileView::OnPlotFFTRow (wxCommandEvent& event)
1212 {
1213   int xCursor, yCursor;
1214   if (! m_canvas->GetCurrentCursor (xCursor, yCursor)) {
1215     wxMessageBox ("No row selected. Please use left mouse button on image to select column","Error");
1216     return;
1217   }
1218   
1219   const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
1220   ImageFileArrayConst v = rIF.getArray();
1221   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1222   int nx = rIF.nx();
1223   int ny = rIF.ny();
1224   
1225   if (v != NULL && yCursor < ny) {
1226     fftw_complex* pcIn = new fftw_complex [nx];
1227
1228     int i;
1229     for (i = 0; i < nx; i++) {
1230       pcIn[i].re = v[i][yCursor];
1231       if (rIF.isComplex())
1232         pcIn[i].im = vImag[i][yCursor];
1233       else
1234         pcIn[i].im = 0;
1235     }
1236
1237     fftw_plan plan = fftw_create_plan (nx, FFTW_FORWARD, FFTW_IN_PLACE);
1238     fftw_one (plan, pcIn, NULL);
1239     fftw_destroy_plan (plan);
1240
1241     double* pX = new double [nx];
1242     double* pYReal = new double [nx];
1243     double* pYImag = new double [nx];
1244     double* pYMag = new double [nx];
1245     for (i = 0; i < nx; i++) {
1246       pX[i] = i;
1247       pYReal[i] = pcIn[i].re;
1248       pYImag[i] = pcIn[i].im;
1249       pYMag[i] = ::sqrt (pcIn[i].re * pcIn[i].re + pcIn[i].im * pcIn[i].im);
1250     }
1251     Fourier::shuffleFourierToNaturalOrder (pYReal, nx);
1252     Fourier::shuffleFourierToNaturalOrder (pYImag, nx);
1253     Fourier::shuffleFourierToNaturalOrder (pYMag, nx);
1254
1255     PlotFileDocument* pPlotDoc = dynamic_cast<PlotFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.plt", wxDOC_SILENT));
1256     if (! pPlotDoc) {
1257       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1258     } else {
1259       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1260       std::ostringstream os;
1261       os << "Row " << yCursor;
1262       std::string title("title ");
1263       title += os.str();
1264       rPlotFile.addEzsetCommand (title.c_str());
1265       rPlotFile.addEzsetCommand ("xlabel Column");
1266       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1267       rPlotFile.addEzsetCommand ("lxfrac 0");
1268       rPlotFile.addEzsetCommand ("curve 1");
1269       rPlotFile.addEzsetCommand ("color 1");
1270        rPlotFile.addEzsetCommand ("dash 1");
1271         rPlotFile.addEzsetCommand ("curve 2");
1272         rPlotFile.addEzsetCommand ("color 4");
1273         rPlotFile.addEzsetCommand ("dash 3");
1274         rPlotFile.addEzsetCommand ("curve 3");
1275         rPlotFile.addEzsetCommand ("color 0");
1276         rPlotFile.addEzsetCommand ("solid");
1277        rPlotFile.addEzsetCommand ("box");
1278       rPlotFile.addEzsetCommand ("grid");
1279       rPlotFile.setCurveSize (4, nx);
1280       rPlotFile.addColumn (0, pX);
1281       rPlotFile.addColumn (1, pYReal);
1282       rPlotFile.addColumn (2, pYImag);
1283       rPlotFile.addColumn (3, pYMag);
1284       for (int iL = 0; iL < rIF.nLabels(); iL++)
1285         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1286       os << " FFT Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1287       *theApp->getLog() << os.str().c_str() << "\n";
1288       rPlotFile.addDescription (os.str().c_str());
1289     }
1290     delete pX;
1291     delete pYReal;
1292     delete pYImag;
1293     delete pYMag;
1294     delete [] pcIn;
1295
1296     if (theApp->getSetModifyNewDocs())
1297       pPlotDoc->Modify(true);
1298     pPlotDoc->UpdateAllViews();
1299   }
1300 }
1301
1302 void
1303 ImageFileView::OnPlotFFTCol (wxCommandEvent& event)
1304 {
1305   int xCursor, yCursor;
1306   if (! m_canvas->GetCurrentCursor (xCursor, yCursor)) {
1307     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1308     return;
1309   }
1310   
1311   const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
1312   ImageFileArrayConst v = rIF.getArray();
1313   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1314   int nx = rIF.nx();
1315   int ny = rIF.ny();
1316   
1317   if (v != NULL && xCursor < nx) {
1318     fftw_complex* pcIn = new fftw_complex [ny];
1319     double *pdTemp = new double [ny];
1320
1321     int i;
1322     for (i = 0; i < ny; i++)
1323       pdTemp[i] = v[xCursor][i];
1324     Fourier::shuffleNaturalToFourierOrder (pdTemp, ny);
1325     for (i = 0; i < ny; i++) 
1326       pcIn[i].re = pdTemp[i];
1327
1328     for (i = 0; i < ny; i++) {
1329       if (rIF.isComplex())
1330         pdTemp[i] = vImag[xCursor][i];
1331       else
1332       pdTemp[i] = 0;
1333     }
1334     Fourier::shuffleNaturalToFourierOrder (pdTemp, ny);
1335     for (i = 0; i < ny; i++)
1336       pcIn[i].im = pdTemp[i];
1337
1338     fftw_plan plan = fftw_create_plan (ny, FFTW_BACKWARD, FFTW_IN_PLACE);
1339     fftw_one (plan, pcIn, NULL);
1340     fftw_destroy_plan (plan);
1341
1342     double* pX = new double [ny];
1343     double* pYReal = new double [ny];
1344     double* pYImag = new double [ny];
1345     double* pYMag = new double [ny];
1346     for (i = 0; i < ny; i++) {
1347       pX[i] = i;
1348       pYReal[i] = pcIn[i].re;
1349       pYImag[i] = pcIn[i].im;
1350       pYMag[i] = ::sqrt (pcIn[i].re * pcIn[i].re + pcIn[i].im * pcIn[i].im);
1351     }
1352
1353     PlotFileDocument* pPlotDoc = dynamic_cast<PlotFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.plt", wxDOC_SILENT));
1354     if (! pPlotDoc) {
1355       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1356     } else {
1357       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1358       std::ostringstream os;
1359       os << "Column " << xCursor;
1360       std::string title("title ");
1361       title += os.str();
1362       rPlotFile.addEzsetCommand (title.c_str());
1363       rPlotFile.addEzsetCommand ("xlabel Column");
1364       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1365       rPlotFile.addEzsetCommand ("lxfrac 0");
1366       rPlotFile.addEzsetCommand ("curve 1");
1367       rPlotFile.addEzsetCommand ("color 1");
1368        rPlotFile.addEzsetCommand ("dash 1");
1369         rPlotFile.addEzsetCommand ("curve 2");
1370         rPlotFile.addEzsetCommand ("color 4");
1371         rPlotFile.addEzsetCommand ("dash 3");
1372         rPlotFile.addEzsetCommand ("curve 3");
1373         rPlotFile.addEzsetCommand ("color 0");
1374         rPlotFile.addEzsetCommand ("solid");
1375        rPlotFile.addEzsetCommand ("box");
1376       rPlotFile.addEzsetCommand ("grid");
1377       rPlotFile.setCurveSize (4, ny);
1378       rPlotFile.addColumn (0, pX);
1379       rPlotFile.addColumn (1, pYReal);
1380       rPlotFile.addColumn (2, pYImag);
1381       rPlotFile.addColumn (3, pYMag);
1382       for (int iL = 0; iL < rIF.nLabels(); iL++)
1383         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1384       os << " FFT Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1385       *theApp->getLog() << os.str().c_str() << "\n";
1386       rPlotFile.addDescription (os.str().c_str());
1387     }
1388     delete pX;
1389     delete pYReal;
1390     delete pYImag;
1391     delete pYMag;
1392     delete pdTemp;
1393     delete [] pcIn;
1394
1395     if (theApp->getSetModifyNewDocs())
1396       pPlotDoc->Modify(true);
1397     pPlotDoc->UpdateAllViews();
1398   }
1399 }
1400 #endif
1401
1402 void
1403 ImageFileView::OnCompareCol (wxCommandEvent& event)
1404 {
1405   int xCursor, yCursor;
1406   if (! m_canvas->GetCurrentCursor (xCursor, yCursor)) {
1407     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1408     return;
1409   }
1410   
1411   std::vector<ImageFileDocument*> vecIFDoc;
1412   theApp->getCompatibleImages (GetDocument(), vecIFDoc);
1413   if (vecIFDoc.size() == 0) {
1414     wxMessageBox ("No compatible images for Column Comparison", "Error");
1415     return;
1416   }
1417   DialogGetComparisonImage dialogGetCompare (m_frame, "Get Comparison Image", vecIFDoc, false);
1418   
1419   if (dialogGetCompare.ShowModal() == wxID_OK) {
1420     ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
1421     const ImageFile& rIF = GetDocument()->getImageFile();
1422     const ImageFile& rCompareIF = pCompareDoc->getImageFile();
1423     
1424     ImageFileArrayConst v1 = rIF.getArray();
1425     ImageFileArrayConst v2 = rCompareIF.getArray();
1426     int nx = rIF.nx();
1427     int ny = rIF.ny();
1428     
1429     if (v1 != NULL && xCursor < nx) {
1430       double* pX = new double [ny];
1431       double* pY1 = new double [ny];
1432       double* pY2 = new double [ny];
1433       for (int i = 0; i < ny; i++) {
1434         pX[i] = i;
1435         pY1[i] = v1[xCursor][i];
1436         pY2[i] = v2[xCursor][i];
1437       }
1438       PlotFileDocument* pPlotDoc = dynamic_cast<PlotFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.plt", wxDOC_SILENT));
1439       if (! pPlotDoc) {
1440         sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1441       } else {
1442         PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1443         std::ostringstream os;
1444         os << "Column " << xCursor << " Comparison";
1445         std::string title("title ");
1446         title += os.str();
1447         rPlotFile.addEzsetCommand (title.c_str());
1448         rPlotFile.addEzsetCommand ("xlabel Row");
1449         rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1450         rPlotFile.addEzsetCommand ("lxfrac 0");
1451         rPlotFile.addEzsetCommand ("curve 1");
1452         rPlotFile.addEzsetCommand ("color 2");
1453         rPlotFile.addEzsetCommand ("curve 2");
1454         rPlotFile.addEzsetCommand ("color 4");
1455         rPlotFile.addEzsetCommand ("dash 5");
1456         rPlotFile.addEzsetCommand ("box");
1457         rPlotFile.addEzsetCommand ("grid");
1458         rPlotFile.setCurveSize (3, ny);
1459         rPlotFile.addColumn (0, pX);
1460         rPlotFile.addColumn (1, pY1);
1461         rPlotFile.addColumn (2, pY2);
1462
1463         unsigned int iL;
1464         for (iL = 0; iL < rIF.nLabels(); iL++) {
1465           std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1466           s += ": ";
1467           s += rIF.labelGet(iL).getLabelString();
1468           rPlotFile.addDescription (s.c_str());
1469         }
1470         for (iL = 0; iL < rCompareIF.nLabels(); iL++) {
1471           std::string s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1472           s += ": ";
1473           s += rCompareIF.labelGet(iL).getLabelString();
1474           rPlotFile.addDescription (s.c_str());
1475         }
1476         os << " Between " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and "
1477           << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1478         *theApp->getLog() << os.str().c_str() << "\n";
1479         rPlotFile.addDescription (os.str().c_str());
1480       }
1481       delete pX;
1482       delete pY1;
1483       delete pY2;
1484       if (theApp->getSetModifyNewDocs())
1485         pPlotDoc->Modify(true);
1486       pPlotDoc->UpdateAllViews();
1487     }
1488   }
1489 }
1490
1491 void
1492 ImageFileView::OnCompareRow (wxCommandEvent& event)
1493 {
1494   int xCursor, yCursor;
1495   if (! m_canvas->GetCurrentCursor (xCursor, yCursor)) {
1496     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1497     return;
1498   }
1499   
1500   std::vector<ImageFileDocument*> vecIFDoc;
1501   theApp->getCompatibleImages (GetDocument(), vecIFDoc);
1502   
1503   if (vecIFDoc.size() == 0) {
1504     wxMessageBox ("No compatible images for Row Comparison", "Error");
1505     return;
1506   }
1507   
1508   DialogGetComparisonImage dialogGetCompare (m_frame, "Get Comparison Image", vecIFDoc, false);
1509   
1510   if (dialogGetCompare.ShowModal() == wxID_OK) {
1511     ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
1512     const ImageFile& rIF = GetDocument()->getImageFile();
1513     const ImageFile& rCompareIF = pCompareDoc->getImageFile();
1514     
1515     ImageFileArrayConst v1 = rIF.getArray();
1516     ImageFileArrayConst v2 = rCompareIF.getArray();
1517     int nx = rIF.nx();
1518     int ny = rIF.ny();
1519     
1520     if (v1 != NULL && yCursor < ny) {
1521       double* pX = new double [nx];
1522       double* pY1 = new double [nx];
1523       double* pY2 = new double [nx];
1524       for (int i = 0; i < nx; i++) {
1525         pX[i] = i;
1526         pY1[i] = v1[i][yCursor];
1527         pY2[i] = v2[i][yCursor];
1528       }
1529       PlotFileDocument* pPlotDoc = dynamic_cast<PlotFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.plt", wxDOC_SILENT));
1530       if (! pPlotDoc) {
1531         sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1532       } else {
1533         PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1534         std::ostringstream os;
1535         os << "Row " << yCursor << " Comparison";
1536         std::string title("title ");
1537         title += os.str();
1538         rPlotFile.addEzsetCommand (title.c_str());
1539         rPlotFile.addEzsetCommand ("xlabel Column");
1540         rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1541         rPlotFile.addEzsetCommand ("lxfrac 0");
1542         rPlotFile.addEzsetCommand ("curve 1");
1543         rPlotFile.addEzsetCommand ("color 2");
1544         rPlotFile.addEzsetCommand ("curve 2");
1545         rPlotFile.addEzsetCommand ("color 4");
1546         rPlotFile.addEzsetCommand ("dash 5");
1547         rPlotFile.addEzsetCommand ("box");
1548         rPlotFile.addEzsetCommand ("grid");
1549         rPlotFile.setCurveSize (3, nx);
1550         rPlotFile.addColumn (0, pX);
1551         rPlotFile.addColumn (1, pY1);
1552         rPlotFile.addColumn (2, pY2);
1553         unsigned int iL;
1554         for (iL = 0; iL < rIF.nLabels(); iL++) {
1555           std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1556           s += ": ";
1557           s += rIF.labelGet(iL).getLabelString();
1558           rPlotFile.addDescription (s.c_str());
1559         }
1560         for (iL = 0; iL < rCompareIF.nLabels(); iL++) {
1561           std::string s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1562           s += ": ";
1563           s += rCompareIF.labelGet(iL).getLabelString();
1564           rPlotFile.addDescription (s.c_str());
1565         }
1566         os << " Between " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and "
1567           << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1568         *theApp->getLog() << os.str().c_str() << "\n";
1569         rPlotFile.addDescription (os.str().c_str());
1570       }
1571       delete pX;
1572       delete pY1;
1573       delete pY2;
1574       if (theApp->getSetModifyNewDocs())
1575         pPlotDoc->Modify(true);
1576       pPlotDoc->UpdateAllViews();
1577     }
1578   }
1579 }
1580
1581 static int NUMBER_HISTOGRAM_BINS = 256;
1582
1583 void
1584 ImageFileView::OnPlotHistogram (wxCommandEvent& event)
1585
1586   const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
1587   ImageFileArrayConst v = rIF.getArray();
1588   int nx = rIF.nx();
1589   int ny = rIF.ny();
1590   
1591   if (v != NULL && nx > 0 && ny > 0) {
1592     PlotFileDocument* pPlotDoc = dynamic_cast<PlotFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.plt", wxDOC_SILENT));
1593     if (! pPlotDoc) {
1594       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1595       return;
1596     }
1597
1598     double* pX = new double [NUMBER_HISTOGRAM_BINS];
1599     double* pY = new double [NUMBER_HISTOGRAM_BINS];
1600     double dMin, dMax;
1601     rIF.getMinMax (dMin, dMax);
1602     double dBinWidth = (dMax - dMin) / NUMBER_HISTOGRAM_BINS;
1603
1604     for (int i = 0; i < NUMBER_HISTOGRAM_BINS; i++) {
1605       pX[i] = dMin + (i + 0.5) * dBinWidth;
1606       pY[i] = 0;
1607     }
1608     for (int ix = 0; ix < nx; ix++)
1609       for (int iy = 0; iy < ny; iy++) {
1610         int iBin = nearest<int> ((v[ix][iy] - dMin) / dBinWidth);
1611         if (iBin >= 0 && iBin < NUMBER_HISTOGRAM_BINS)
1612           pY[iBin] += 1;
1613       }
1614
1615     PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1616     std::ostringstream os;
1617     os << "Histogram";
1618     std::string title("title ");
1619     title += os.str();
1620     rPlotFile.addEzsetCommand (title.c_str());
1621     rPlotFile.addEzsetCommand ("xlabel Pixel Value");
1622     rPlotFile.addEzsetCommand ("ylabel Count");
1623     rPlotFile.addEzsetCommand ("box");
1624     rPlotFile.addEzsetCommand ("grid");
1625     rPlotFile.setCurveSize (2, NUMBER_HISTOGRAM_BINS);
1626     rPlotFile.addColumn (0, pX);
1627     rPlotFile.addColumn (1, pY);
1628     for (unsigned int iL = 0; iL < rIF.nLabels(); iL++) {
1629       std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1630       s += ": ";
1631       s += rIF.labelGet(iL).getLabelString();
1632       rPlotFile.addDescription (s.c_str());
1633     }
1634     os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1635     *theApp->getLog() << os.str().c_str() << "\n";
1636     rPlotFile.addDescription (os.str().c_str());
1637     delete pX;
1638     delete pY;
1639     if (theApp->getSetModifyNewDocs())
1640       pPlotDoc->Modify(true);
1641     pPlotDoc->UpdateAllViews();
1642   }
1643 }
1644
1645
1646 // PhantomCanvas
1647
1648 PhantomCanvas::PhantomCanvas (PhantomView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
1649 : wxScrolledWindow(frame, -1, pos, size, style)
1650 {
1651   m_pView = v;
1652 }
1653
1654 void 
1655 PhantomCanvas::OnDraw (wxDC& dc)
1656 {
1657   if (m_pView)
1658     m_pView->OnDraw(& dc);
1659 }
1660
1661
1662 // PhantomView
1663
1664 IMPLEMENT_DYNAMIC_CLASS(PhantomView, wxView)
1665
1666 BEGIN_EVENT_TABLE(PhantomView, wxView)
1667 EVT_MENU(PHMMENU_FILE_PROPERTIES, PhantomView::OnProperties)
1668 EVT_MENU(PHMMENU_PROCESS_RASTERIZE, PhantomView::OnRasterize)
1669 EVT_MENU(PHMMENU_PROCESS_PROJECTIONS, PhantomView::OnProjections)
1670 END_EVENT_TABLE()
1671
1672 PhantomView::PhantomView(void) 
1673 : wxView(), m_canvas(NULL), m_frame(NULL)
1674 {
1675   m_iDefaultNDet = 367;
1676   m_iDefaultNView = 320;
1677   m_iDefaultNSample = 2;
1678   m_dDefaultRotation = 1;
1679   m_dDefaultFocalLength = 2;
1680   m_dDefaultFieldOfView = 1;
1681   m_iDefaultGeometry = Scanner::GEOMETRY_PARALLEL;
1682   m_iDefaultTrace = Trace::TRACE_NONE;
1683
1684   m_iDefaultRasterNX = 256;
1685   m_iDefaultRasterNY = 256;
1686   m_iDefaultRasterNSamples = 2;
1687 }
1688
1689 PhantomView::~PhantomView(void)
1690 {
1691 }
1692
1693 void
1694 PhantomView::OnProperties (wxCommandEvent& event)
1695 {
1696   const int idPhantom = GetDocument()->getPhantomID();
1697   const wxString& namePhantom = GetDocument()->getPhantomName();
1698   std::ostringstream os;
1699   os << "Phantom " << namePhantom.c_str() << " (" << idPhantom << ")" << "\n";
1700   const Phantom& rPhantom = GetDocument()->getPhantom();
1701   rPhantom.printDefinitions (os);
1702 #if DEBUG
1703   rPhantom.print (os);
1704 #endif
1705   *theApp->getLog() << os.str().c_str() << "\n";
1706   wxMessageBox (os.str().c_str(), "Phantom Properties");
1707 }
1708
1709
1710 void
1711 PhantomView::OnProjections (wxCommandEvent& event)
1712 {
1713   DialogGetProjectionParameters dialogProjection (m_frame, m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, m_dDefaultRotation, m_dDefaultFocalLength, m_dDefaultFieldOfView, m_iDefaultGeometry, m_iDefaultTrace);
1714   int retVal = dialogProjection.ShowModal();
1715   if (retVal == wxID_OK) {
1716     m_iDefaultNDet = dialogProjection.getNDet();
1717     m_iDefaultNView = dialogProjection.getNView();
1718     m_iDefaultNSample = dialogProjection.getNSamples();
1719     m_iDefaultTrace = dialogProjection.getTrace();
1720     m_dDefaultRotation = dialogProjection.getRotAngle();
1721     m_dDefaultFocalLength = dialogProjection.getFocalLengthRatio();
1722     m_dDefaultFieldOfView = dialogProjection.getFieldOfViewRatio();
1723     wxString sGeometry = dialogProjection.getGeometry();
1724     m_iDefaultGeometry = Scanner::convertGeometryNameToID (sGeometry.c_str());
1725     
1726     if (m_iDefaultNDet > 0 && m_iDefaultNView > 0 && sGeometry != "") {
1727       const Phantom& rPhantom = GetDocument()->getPhantom();
1728       ProjectionFileDocument* pProjectionDoc = dynamic_cast<ProjectionFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.pj", wxDOC_SILENT));
1729       if (! pProjectionDoc) {
1730         sys_error (ERR_SEVERE, "Unable to create projection document");
1731         return;
1732       }
1733       Projections& rProj = pProjectionDoc->getProjections();
1734       Scanner theScanner (rPhantom, sGeometry.c_str(), m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, m_dDefaultRotation, m_dDefaultFocalLength, m_dDefaultFieldOfView);
1735       if (theScanner.fail()) {
1736         *theApp->getLog() << "Failed making scanner: " << theScanner.failMessage().c_str() << "\n";
1737         return;
1738       }
1739       rProj.initFromScanner (theScanner);
1740       m_dDefaultRotation /= PI;  // convert back to PI units
1741       
1742       Timer timer;
1743       if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
1744         ProjectionsDialog dialogProjections (theScanner, rProj, rPhantom, m_iDefaultTrace, dynamic_cast<wxWindow*>(m_frame));
1745         for (int iView = 0; iView < rProj.nView(); iView++) {
1746           ::wxYield();
1747           ::wxYield();
1748           if (dialogProjections.isCancelled() || ! dialogProjections.projectView (iView)) {
1749             pProjectionDoc->DeleteAllViews();
1750             return;
1751           }
1752           ::wxYield();
1753           ::wxYield();
1754           while (dialogProjections.isPaused()) {
1755             ::wxYield();
1756             ::wxUsleep(50);
1757           }
1758         }
1759       } else {
1760         wxProgressDialog dlgProgress (wxString("Projection"), wxString("Projection Progress"), rProj.nView() + 1, m_frame, wxPD_CAN_ABORT);
1761         for (int i = 0; i < rProj.nView(); i++) {
1762           theScanner.collectProjections (rProj, rPhantom, i, 1, true, m_iDefaultTrace);
1763           if (! dlgProgress.Update (i+1)) {
1764             pProjectionDoc->DeleteAllViews();
1765             return;
1766           }
1767         }
1768       }
1769       
1770       std::ostringstream os;
1771       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();
1772       rProj.setCalcTime (timer.timerEnd());
1773       rProj.setRemark (os.str());
1774       *theApp->getLog() << os.str().c_str() << "\n";
1775       
1776       m_frame->Lower();
1777       ::wxYield();
1778       ProjectionFileView* projView = dynamic_cast<ProjectionFileView*>(pProjectionDoc->GetFirstView());
1779       if (projView) {
1780         projView->getFrame()->SetFocus();
1781         projView->OnUpdate (projView, NULL);
1782       }
1783       if (wxView* pView = pProjectionDoc->GetFirstView()) {
1784         if (wxFrame* pFrame = pView->GetFrame()) {
1785           pFrame->SetFocus();
1786           pFrame->Raise();
1787         }
1788         theApp->getDocManager()->ActivateView (pView, true, false);
1789       }
1790       ::wxYield();
1791       if (theApp->getSetModifyNewDocs())
1792         pProjectionDoc->Modify(true);
1793       pProjectionDoc->UpdateAllViews(this);
1794     }
1795   }
1796 }
1797
1798
1799 void
1800 PhantomView::OnRasterize (wxCommandEvent& event)
1801 {
1802   DialogGetRasterParameters dialogRaster (m_frame, m_iDefaultRasterNX, m_iDefaultRasterNY, m_iDefaultRasterNSamples);
1803   int retVal = dialogRaster.ShowModal();
1804   if (retVal == wxID_OK) {
1805     m_iDefaultRasterNX = dialogRaster.getXSize();
1806     m_iDefaultRasterNY  = dialogRaster.getYSize();
1807     m_iDefaultRasterNSamples = dialogRaster.getNSamples();
1808     if (m_iDefaultRasterNSamples < 1)
1809       m_iDefaultRasterNSamples = 1;
1810     if (m_iDefaultRasterNX > 0 && m_iDefaultRasterNY > 0) {
1811       const Phantom& rPhantom = GetDocument()->getPhantom();
1812       ImageFileDocument* pRasterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.if", wxDOC_SILENT));
1813       if (! pRasterDoc) {
1814         sys_error (ERR_SEVERE, "Unable to create image file");
1815         return;
1816       }
1817       ImageFile& imageFile = pRasterDoc->getImageFile();
1818       
1819       imageFile.setArraySize (m_iDefaultRasterNX, m_iDefaultRasterNX);
1820       wxProgressDialog dlgProgress (wxString("Rasterize"), wxString("Rasterization Progress"), imageFile.nx() + 1, m_frame, wxPD_CAN_ABORT);
1821       Timer timer;
1822       for (unsigned int i = 0; i < imageFile.nx(); i++) {
1823         rPhantom.convertToImagefile (imageFile, m_iDefaultRasterNSamples, Trace::TRACE_NONE, i, 1, true);
1824         if (! dlgProgress.Update(i+1)) {
1825           pRasterDoc->DeleteAllViews();
1826           return;
1827         }
1828       }
1829       if (theApp->getSetModifyNewDocs())
1830         pRasterDoc->Modify(true);
1831       pRasterDoc->UpdateAllViews(this);
1832       std::ostringstream os;
1833       os << "Rasterize Phantom " << rPhantom.name() << ": XSize=" << m_iDefaultRasterNX << ", YSize=" 
1834         << m_iDefaultRasterNY << ", nSamples=" << m_iDefaultRasterNSamples;
1835       *theApp->getLog() << os.str().c_str() << "\n";
1836       imageFile.labelAdd (os.str().c_str(), timer.timerEnd());
1837       ImageFileView* rasterView = dynamic_cast<ImageFileView*>(pRasterDoc->GetFirstView());
1838       if (rasterView) {
1839         rasterView->getFrame()->SetFocus();
1840         rasterView->OnUpdate (rasterView, NULL);
1841       }
1842       
1843     }
1844   }
1845 }
1846
1847
1848 PhantomCanvas* 
1849 PhantomView::CreateCanvas (wxView *view, wxFrame *parent)
1850 {
1851   PhantomCanvas* pCanvas;
1852   int width, height;
1853   parent->GetClientSize(&width, &height);
1854   
1855   pCanvas = new PhantomCanvas (dynamic_cast<PhantomView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
1856   
1857   pCanvas->SetBackgroundColour(*wxWHITE);
1858   pCanvas->Clear();
1859   
1860   return pCanvas;
1861 }
1862
1863 wxFrame*
1864 PhantomView::CreateChildFrame(wxDocument *doc, wxView *view)
1865 {
1866 #if CTSIM_MDI
1867   wxMDIChildFrame *subframe = new wxMDIChildFrame(theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(256, 256), wxDEFAULT_FRAME_STYLE);
1868 #else
1869   wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(256, 256), wxDEFAULT_FRAME_STYLE);
1870 #endif
1871
1872   wxMenu *file_menu = new wxMenu;
1873   
1874   file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
1875   file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...");
1876   file_menu->Append(wxID_OPEN, "&Open...");
1877   file_menu->Append(wxID_SAVEAS, "Save &As...");
1878   file_menu->Append(wxID_CLOSE, "&Close");
1879   
1880   file_menu->AppendSeparator();
1881   file_menu->Append(PHMMENU_FILE_PROPERTIES, "P&roperties");
1882   
1883   file_menu->AppendSeparator();
1884   file_menu->Append(wxID_PRINT, "&Print...");
1885   file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
1886   file_menu->Append(wxID_PREVIEW, "Print Pre&view");
1887   
1888   wxMenu *process_menu = new wxMenu;
1889   process_menu->Append(PHMMENU_PROCESS_RASTERIZE, "&Rasterize...");
1890   process_menu->Append(PHMMENU_PROCESS_PROJECTIONS, "&Projections...");
1891   
1892   wxMenu *help_menu = new wxMenu;
1893   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
1894   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
1895   
1896   wxMenuBar *menu_bar = new wxMenuBar;
1897   
1898   menu_bar->Append(file_menu, "&File");
1899   menu_bar->Append(process_menu, "&Process");
1900   menu_bar->Append(help_menu, "&Help");
1901   
1902   subframe->SetMenuBar(menu_bar);
1903   
1904   subframe->Centre(wxBOTH);
1905   
1906   return subframe;
1907 }
1908
1909
1910 bool 
1911 PhantomView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
1912 {
1913   m_frame = CreateChildFrame(doc, this);
1914   SetFrame(m_frame);
1915   
1916   int width, height;
1917   m_frame->GetClientSize(&width, &height);
1918   m_frame->SetTitle("PhantomView");
1919   m_canvas = CreateCanvas(this, m_frame);
1920   
1921 #ifdef __X__
1922   int x, y;  // X requires a forced resize
1923   m_frame->GetSize(&x, &y);
1924   m_frame->SetSize(-1, -1, x, y);
1925 #endif
1926   
1927   m_frame->Show(true);
1928   Activate(true);
1929   
1930   return true;
1931 }
1932
1933
1934 void 
1935 PhantomView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
1936 {
1937   if (m_canvas)
1938     m_canvas->Refresh();
1939 }
1940
1941 bool 
1942 PhantomView::OnClose (bool deleteWindow)
1943 {
1944   if (!GetDocument()->Close())
1945     return false;
1946   
1947 //  m_canvas->Clear();
1948   m_canvas->m_pView = NULL;
1949   m_canvas = NULL;
1950 //  wxString s(wxTheApp->GetAppName());
1951 //  if (m_frame)
1952 //    m_frame->SetTitle(s);
1953   SetFrame(NULL);
1954   
1955   Activate(false);
1956   
1957   if (deleteWindow) {
1958
1959     delete m_frame;
1960     return true;
1961   }
1962   return true;
1963 }
1964
1965 void
1966 PhantomView::OnDraw (wxDC* dc)
1967 {
1968   int xsize, ysize;
1969   m_canvas->GetClientSize (&xsize, &ysize);
1970   SGPDriver driver (dc, xsize, ysize);
1971   SGP sgp (driver);
1972   const Phantom& rPhantom = GetDocument()->getPhantom();
1973   sgp.setColor (C_RED);
1974   rPhantom.show (sgp);
1975 }
1976
1977 // ProjectionCanvas
1978
1979 ProjectionFileCanvas::ProjectionFileCanvas (ProjectionFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
1980 : wxScrolledWindow(frame, -1, pos, size, style)
1981 {
1982   m_pView = v;
1983 }
1984
1985 void 
1986 ProjectionFileCanvas::OnDraw(wxDC& dc)
1987 {
1988   if (m_pView)
1989     m_pView->OnDraw(& dc);
1990 }
1991
1992 // ProjectionFileView
1993
1994 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileView, wxView)
1995
1996 BEGIN_EVENT_TABLE(ProjectionFileView, wxView)
1997 EVT_MENU(PJMENU_FILE_PROPERTIES, ProjectionFileView::OnProperties)
1998 EVT_MENU(PJMENU_RECONSTRUCT_FBP, ProjectionFileView::OnReconstructFBP)
1999 EVT_MENU(PJMENU_CONVERT_POLAR, ProjectionFileView::OnConvertPolar)
2000 EVT_MENU(PJMENU_CONVERT_FFT_POLAR, ProjectionFileView::OnConvertFFTPolar)
2001 END_EVENT_TABLE()
2002
2003 ProjectionFileView::ProjectionFileView(void) 
2004 : wxView(), m_canvas(NULL), m_frame(NULL)
2005 {
2006   m_iDefaultNX = 256;
2007   m_iDefaultNY = 256;
2008   m_iDefaultFilter = SignalFilter::FILTER_ABS_BANDLIMIT;
2009   m_dDefaultFilterParam = 1.;
2010 #if HAVE_FFTW
2011   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_RFFTW;
2012   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_DIRECT;
2013 #else
2014   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_CONVOLUTION;
2015   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_DIRECT;
2016 #endif
2017   m_iDefaultZeropad = 1;
2018   m_iDefaultBackprojector = Backprojector::BPROJ_IDIFF3;
2019   m_iDefaultInterpolation = Backprojector::INTERP_LINEAR;
2020   m_iDefaultInterpParam = 1;
2021   m_iDefaultTrace = Trace::TRACE_NONE;
2022
2023   m_iDefaultPolarNX = 256;
2024   m_iDefaultPolarNY = 256;
2025   m_iDefaultPolarInterpolation = Projections::POLAR_INTERP_BILINEAR;
2026   m_iDefaultPolarZeropad = 1;
2027 }
2028
2029 ProjectionFileView::~ProjectionFileView(void)
2030 {
2031 }
2032
2033 void
2034 ProjectionFileView::OnProperties (wxCommandEvent& event)
2035 {
2036   const Projections& rProj = GetDocument()->getProjections();
2037   std::ostringstream os;
2038   rProj.printScanInfo(os);
2039   *theApp->getLog() << os.str().c_str();
2040   wxMessageDialog dialogMsg (m_frame, os.str().c_str(), "Projection File Properties", wxOK | wxICON_INFORMATION);
2041   dialogMsg.ShowModal();
2042 }
2043
2044
2045 void
2046 ProjectionFileView::OnConvertPolar (wxCommandEvent& event)
2047 {
2048   Projections& rProj = GetDocument()->getProjections();
2049   DialogGetConvertPolarParameters dialogPolar (m_frame, "Convert Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2050     m_iDefaultPolarInterpolation, -1);
2051   if (dialogPolar.ShowModal() == wxID_OK) {
2052     wxString strInterpolation (dialogPolar.getInterpolationName());
2053     m_iDefaultPolarNX = dialogPolar.getXSize();
2054     m_iDefaultPolarNY = dialogPolar.getYSize();
2055     ImageFileDocument* pPolarDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.if", wxDOC_SILENT));
2056     ImageFile& rIF = pPolarDoc->getImageFile();
2057     if (! pPolarDoc) {
2058       sys_error (ERR_SEVERE, "Unable to create image file");
2059       return;
2060     }
2061     rIF.setArraySize (m_iDefaultPolarNX, m_iDefaultPolarNY);
2062     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2063     rProj.convertPolar (rIF, m_iDefaultPolarInterpolation);
2064     rIF.labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2065     std::ostringstream os;
2066     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to polar image: xSize=" 
2067       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2068       << strInterpolation.c_str();
2069     *theApp->getLog() << os.str().c_str() << "\n";
2070     rIF.labelAdd (os.str().c_str());
2071     if (theApp->getSetModifyNewDocs())
2072       pPolarDoc->Modify(true);
2073     pPolarDoc->UpdateAllViews();
2074     pPolarDoc->GetFirstView()->OnUpdate (this, NULL);
2075   }
2076 }
2077
2078 void
2079 ProjectionFileView::OnConvertFFTPolar (wxCommandEvent& event)
2080 {
2081   Projections& rProj = GetDocument()->getProjections();
2082   DialogGetConvertPolarParameters dialogPolar (m_frame, "Convert to FFT Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2083     m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
2084   if (dialogPolar.ShowModal() == wxID_OK) {
2085     wxString strInterpolation (dialogPolar.getInterpolationName());
2086     m_iDefaultPolarNX = dialogPolar.getXSize();
2087     m_iDefaultPolarNY = dialogPolar.getYSize();
2088     m_iDefaultPolarZeropad = dialogPolar.getZeropad();
2089     ImageFileDocument* pPolarDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.if", wxDOC_SILENT));
2090     ImageFile& rIF = pPolarDoc->getImageFile();
2091     if (! pPolarDoc) {
2092       sys_error (ERR_SEVERE, "Unable to create image file");
2093       return;
2094     }
2095     rIF.setArraySize (m_iDefaultPolarNX, m_iDefaultPolarNY);
2096     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2097     rProj.convertFFTPolar (rIF, m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
2098     rIF.labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2099     std::ostringstream os;
2100     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to FFT polar image: xSize=" 
2101       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2102       << strInterpolation.c_str() << ", zeropad=" << m_iDefaultPolarZeropad;
2103     *theApp->getLog() << os.str().c_str() << "\n";
2104     rIF.labelAdd (os.str().c_str());
2105     if (theApp->getSetModifyNewDocs())
2106       pPolarDoc->Modify(true);
2107     pPolarDoc->UpdateAllViews();
2108     pPolarDoc->GetFirstView()->OnUpdate (this, NULL);
2109   }}
2110
2111 void
2112 ProjectionFileView::OnReconstructFourier (wxCommandEvent& event)
2113 {
2114   wxMessageBox ("Fourier Reconstruction is not yet supported", "Unimplemented function");
2115 }
2116
2117 void
2118 ProjectionFileView::OnReconstructFBP (wxCommandEvent& event)
2119 {
2120   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);
2121   
2122   int retVal = dialogReconstruction.ShowModal();
2123   if (retVal == wxID_OK) {
2124     m_iDefaultNX = dialogReconstruction.getXSize();
2125     m_iDefaultNY = dialogReconstruction.getYSize();
2126     wxString optFilterName = dialogReconstruction.getFilterName();
2127     m_iDefaultFilter = SignalFilter::convertFilterNameToID (optFilterName.c_str());
2128     m_dDefaultFilterParam = dialogReconstruction.getFilterParam();
2129     wxString optFilterMethodName = dialogReconstruction.getFilterMethodName();
2130     m_iDefaultFilterMethod = ProcessSignal::convertFilterMethodNameToID(optFilterMethodName.c_str());
2131     m_iDefaultZeropad = dialogReconstruction.getZeropad();
2132     wxString optFilterGenerationName = dialogReconstruction.getFilterGenerationName();
2133     m_iDefaultFilterGeneration = ProcessSignal::convertFilterGenerationNameToID (optFilterGenerationName.c_str());
2134     wxString optInterpName = dialogReconstruction.getInterpName();
2135     m_iDefaultInterpolation = Backprojector::convertInterpNameToID (optInterpName.c_str());
2136     m_iDefaultInterpParam = dialogReconstruction.getInterpParam();
2137     wxString optBackprojectName = dialogReconstruction.getBackprojectName();
2138     m_iDefaultBackprojector = Backprojector::convertBackprojectNameToID (optBackprojectName.c_str());
2139     m_iDefaultTrace = dialogReconstruction.getTrace();
2140     if (m_iDefaultNX > 0 && m_iDefaultNY > 0) {
2141       ImageFileDocument* pReconDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.if", wxDOC_SILENT));
2142       if (! pReconDoc) {
2143         sys_error (ERR_SEVERE, "Unable to create image file");
2144         return;
2145       }
2146       ImageFile& imageFile = pReconDoc->getImageFile();
2147       const Projections& rProj = GetDocument()->getProjections();
2148       imageFile.setArraySize (m_iDefaultNX, m_iDefaultNY);
2149       
2150       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);
2151
2152       Timer timerRecon;
2153       if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
2154         ReconstructDialog* pDlgReconstruct = new ReconstructDialog (*pReconstruct, rProj, imageFile, m_iDefaultTrace, m_frame);
2155         for (int iView = 0; iView < rProj.nView(); iView++) {
2156           ::wxYield();
2157           ::wxYield();
2158           if (pDlgReconstruct->isCancelled() || ! pDlgReconstruct->reconstructView (iView)) {
2159             delete pDlgReconstruct;
2160             delete pReconstruct;
2161             pReconDoc->DeleteAllViews();
2162             return;
2163           }
2164           ::wxYield();
2165           ::wxYield();
2166           while (pDlgReconstruct->isPaused()) {
2167             ::wxYield();
2168             ::wxUsleep(50);
2169           }
2170         }
2171         delete pDlgReconstruct;
2172       } else {
2173         wxProgressDialog dlgProgress (wxString("Reconstruction"), wxString("Reconstruction Progress"), rProj.nView() + 1, m_frame, wxPD_CAN_ABORT);
2174         for (int i = 0; i < rProj.nView(); i++) {
2175           pReconstruct->reconstructView (i, 1);
2176           if (! dlgProgress.Update(i + 1)) {
2177             delete pReconstruct;
2178             pReconDoc->DeleteAllViews();
2179             return;
2180           }
2181         }
2182       }
2183       delete pReconstruct;
2184       if (theApp->getSetModifyNewDocs())
2185         pReconDoc->Modify(true);
2186       pReconDoc->UpdateAllViews(this);
2187       ImageFileView* rasterView = dynamic_cast<ImageFileView*>(pReconDoc->GetFirstView());
2188       if (rasterView) {
2189         rasterView->getFrame()->SetFocus();
2190         rasterView->OnUpdate (rasterView, NULL);
2191       }
2192       std::ostringstream os;
2193       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();
2194       *theApp->getLog() << os.str().c_str() << "\n";
2195       imageFile.labelAdd (rProj.getLabel());
2196       imageFile.labelAdd (os.str().c_str(), timerRecon.timerEnd());
2197     }
2198   }
2199 }
2200
2201
2202 ProjectionFileCanvas* 
2203 ProjectionFileView::CreateCanvas (wxView *view, wxFrame *parent)
2204 {
2205   ProjectionFileCanvas* pCanvas;
2206   int width, height;
2207   parent->GetClientSize(&width, &height);
2208   
2209   pCanvas = new ProjectionFileCanvas (dynamic_cast<ProjectionFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
2210   
2211   pCanvas->SetScrollbars(20, 20, 50, 50);
2212   pCanvas->SetBackgroundColour(*wxWHITE);
2213   pCanvas->Clear();
2214   
2215   return pCanvas;
2216 }
2217
2218 wxFrame*
2219 ProjectionFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2220 {
2221 #ifdef CTSIM_MDI
2222   wxMDIChildFrame *subframe = new wxMDIChildFrame (theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2223 #else
2224   wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2225 #endif
2226
2227   wxMenu *file_menu = new wxMenu;
2228   
2229   file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
2230   file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...");
2231   file_menu->Append(wxID_OPEN, "&Open...");
2232   file_menu->Append(wxID_SAVE, "&Save");
2233   file_menu->Append(wxID_SAVEAS, "Save &As...");
2234   file_menu->Append(wxID_CLOSE, "&Close");
2235   
2236   file_menu->AppendSeparator();
2237   file_menu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
2238   
2239   file_menu->AppendSeparator();
2240   file_menu->Append(wxID_PRINT, "&Print...");
2241   file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2242   file_menu->Append(wxID_PREVIEW, "Print Pre&view");
2243   
2244   wxMenu *convert_menu = new wxMenu;
2245   convert_menu->Append (PJMENU_CONVERT_POLAR, "&Polar Image...");
2246   convert_menu->Append (PJMENU_CONVERT_FFT_POLAR, "&FFT->Polar Image...");
2247   
2248   wxMenu *reconstruct_menu = new wxMenu;
2249   reconstruct_menu->Append (PJMENU_RECONSTRUCT_FBP, "&Filtered Backprojection...");
2250   reconstruct_menu->Append (PJMENU_RECONSTRUCT_FOURIER, "&Fourier...");
2251
2252   wxMenu *help_menu = new wxMenu;
2253   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
2254   help_menu->AppendSeparator();
2255   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2256   
2257   wxMenuBar *menu_bar = new wxMenuBar;
2258   
2259   menu_bar->Append (file_menu, "&File");
2260   menu_bar->Append (convert_menu, "&Convert");
2261   menu_bar->Append (reconstruct_menu, "&Reconstruct");
2262   menu_bar->Append (help_menu, "&Help");
2263   
2264   subframe->SetMenuBar(menu_bar);
2265   
2266   subframe->Centre(wxBOTH);
2267   
2268   return subframe;
2269 }
2270
2271
2272 bool 
2273 ProjectionFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2274 {
2275   m_frame = CreateChildFrame(doc, this);
2276   SetFrame(m_frame);
2277   
2278   int width, height;
2279   m_frame->GetClientSize(&width, &height);
2280   m_frame->SetTitle("ProjectionFileView");
2281   m_canvas = CreateCanvas(this, m_frame);
2282   
2283 #ifdef __X__
2284   int x, y;  // X requires a forced resize
2285   m_frame->GetSize(&x, &y);
2286   m_frame->SetSize(-1, -1, x, y);
2287 #endif
2288   
2289   m_frame->Show(true);
2290   Activate(true);
2291   
2292   return true;
2293 }
2294
2295 void 
2296 ProjectionFileView::OnDraw (wxDC* dc)
2297 {
2298   if (m_bitmap.Ok())
2299     dc->DrawBitmap (m_bitmap, 0, 0, false);
2300 }
2301
2302
2303 void 
2304 ProjectionFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2305 {
2306   const Projections& rProj = GetDocument()->getProjections();
2307   const int nDet = rProj.nDet();
2308   const int nView = rProj.nView();
2309   if (nDet != 0 && nView != 0) {
2310     const DetectorArray& detarray = rProj.getDetectorArray(0);
2311     const DetectorValue* detval = detarray.detValues();
2312     double min = detval[0];
2313     double max = detval[0];
2314     for (int iy = 0; iy < nView; iy++) {
2315       const DetectorArray& detarray = rProj.getDetectorArray(iy);
2316       const DetectorValue* detval = detarray.detValues();
2317       for (int ix = 0; ix < nDet; ix++) {
2318         if (min > detval[ix])
2319           min = detval[ix];
2320         else if (max < detval[ix])
2321           max = detval[ix];
2322       }
2323     }
2324     
2325     unsigned char* imageData = new unsigned char [nDet * nView * 3];
2326     double scale = (max - min) / 255;
2327     for (int iy2 = 0; iy2 < nView; iy2++) {
2328       const DetectorArray& detarray = rProj.getDetectorArray (iy2);
2329       const DetectorValue* detval = detarray.detValues();
2330       for (int ix = 0; ix < nDet; ix++) {
2331         int intensity = static_cast<int>(((detval[ix] - min) / scale) + 0.5);
2332         intensity = clamp(intensity, 0, 255);
2333         int baseAddr = (iy2 * nDet + ix) * 3;
2334         imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
2335       }
2336     }
2337     wxImage image (nDet, nView, imageData, true);
2338     m_bitmap = image.ConvertToBitmap();
2339     delete imageData;
2340     int xSize = nDet;
2341     int ySize = nView;
2342     xSize = clamp (xSize, 0, 800);
2343     ySize = clamp (ySize, 0, 800);
2344     m_frame->SetClientSize (xSize, ySize);
2345     m_canvas->SetScrollbars (20, 20, nDet/20, nView/20);
2346   }
2347   
2348   if (m_canvas)
2349     m_canvas->Refresh();
2350 }
2351
2352 bool 
2353 ProjectionFileView::OnClose (bool deleteWindow)
2354 {
2355   if (!GetDocument()->Close())
2356     return false;
2357   
2358   // m_canvas->Clear();
2359   m_canvas->m_pView = NULL;
2360   m_canvas = NULL;
2361   wxString s(wxTheApp->GetAppName());
2362   if (m_frame)
2363     m_frame->SetTitle(s);
2364   SetFrame(NULL);
2365   
2366   Activate(false);
2367   
2368   if (deleteWindow) {
2369     delete m_frame;
2370     return true;
2371   }
2372   return true;
2373 }
2374
2375
2376
2377 // PlotFileCanvas
2378 PlotFileCanvas::PlotFileCanvas (PlotFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2379 : wxScrolledWindow(frame, -1, pos, size, style)
2380 {
2381   m_pView = v;
2382 }
2383
2384 void 
2385 PlotFileCanvas::OnDraw(wxDC& dc)
2386 {
2387   if (m_pView)
2388     m_pView->OnDraw(& dc);
2389 }
2390
2391
2392 // PlotFileView
2393
2394 IMPLEMENT_DYNAMIC_CLASS(PlotFileView, wxView)
2395
2396 BEGIN_EVENT_TABLE(PlotFileView, wxView)
2397 EVT_MENU(PJMENU_FILE_PROPERTIES, PlotFileView::OnProperties)
2398 EVT_MENU(PLOTMENU_VIEW_SCALE_MINMAX, PlotFileView::OnScaleMinMax)
2399 EVT_MENU(PLOTMENU_VIEW_SCALE_AUTO, PlotFileView::OnScaleAuto)
2400 EVT_MENU(PLOTMENU_VIEW_SCALE_FULL, PlotFileView::OnScaleFull)
2401 END_EVENT_TABLE()
2402
2403 PlotFileView::PlotFileView(void) 
2404 : wxView(), m_canvas(NULL), m_frame(NULL), m_pEZPlot(NULL)
2405 {
2406   m_bMinSpecified = false;
2407   m_bMaxSpecified = false;
2408 }
2409
2410 PlotFileView::~PlotFileView(void)
2411 {
2412   if (m_pEZPlot)
2413     delete m_pEZPlot;
2414 }
2415
2416 void
2417 PlotFileView::OnProperties (wxCommandEvent& event)
2418 {
2419   const PlotFile& rPlot = GetDocument()->getPlotFile();
2420   std::ostringstream os;
2421   os << "Columns: " << rPlot.getNumColumns() << ", Records: " << rPlot.getNumRecords() << "\n";
2422   rPlot.printHeadersBrief (os);
2423   *theApp->getLog() << os.str().c_str();
2424   wxMessageDialog dialogMsg (m_frame, os.str().c_str(), "Plot File Properties", wxOK | wxICON_INFORMATION);
2425   dialogMsg.ShowModal();
2426 }
2427
2428
2429 void 
2430 PlotFileView::OnScaleAuto (wxCommandEvent& event)
2431 {
2432   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2433   double min, max, mean, mode, median, stddev;
2434   rPlotFile.statistics (1, min, max, mean, mode, median, stddev);
2435   DialogAutoScaleParameters dialogAutoScale (m_frame, mean, mode, median, stddev, m_dAutoScaleFactor);
2436   int iRetVal = dialogAutoScale.ShowModal();
2437   if (iRetVal == wxID_OK) {
2438     m_bMinSpecified = true;
2439     m_bMaxSpecified = true;
2440     double dMin, dMax;
2441     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
2442       m_dMinPixel = dMin;
2443       m_dMaxPixel = dMax;
2444       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
2445       OnUpdate (this, NULL);
2446     }
2447   }
2448 }
2449
2450 void 
2451 PlotFileView::OnScaleMinMax (wxCommandEvent& event)
2452 {
2453   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2454   double min;
2455   double max;
2456
2457   if (! m_bMinSpecified || ! m_bMaxSpecified) {
2458     if (! rPlotFile.getMinMax (1, min, max)) {
2459       *theApp->getLog() << "Error: unable to find Min/Max\n";
2460       return;
2461     }
2462   }
2463   
2464   if (m_bMinSpecified)
2465     min = m_dMinPixel;
2466   if (m_bMaxSpecified)
2467     max = m_dMaxPixel;
2468   
2469   DialogGetMinMax dialogMinMax (m_frame, "Set Y-axis Minimum & Maximum", min, max);
2470   int retVal = dialogMinMax.ShowModal();
2471   if (retVal == wxID_OK) {
2472     m_bMinSpecified = true;
2473     m_bMaxSpecified = true;
2474     m_dMinPixel = dialogMinMax.getMinimum();
2475     m_dMaxPixel = dialogMinMax.getMaximum();
2476     OnUpdate (this, NULL);
2477   }
2478 }
2479
2480 void 
2481 PlotFileView::OnScaleFull (wxCommandEvent& event)
2482 {
2483   if (m_bMinSpecified || m_bMaxSpecified) {
2484     m_bMinSpecified = false;
2485     m_bMaxSpecified = false;
2486     OnUpdate (this, NULL);
2487   }
2488 }
2489
2490
2491 PlotFileCanvas* 
2492 PlotFileView::CreateCanvas (wxView *view, wxFrame *parent)
2493 {
2494   PlotFileCanvas* pCanvas;
2495   int width, height;
2496   parent->GetClientSize(&width, &height);
2497   
2498   pCanvas = new PlotFileCanvas (dynamic_cast<PlotFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
2499   
2500   pCanvas->SetBackgroundColour(*wxWHITE);
2501   pCanvas->Clear();
2502   
2503   return pCanvas;
2504 }
2505
2506 wxFrame*
2507 PlotFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2508 {
2509 #ifdef CTSIM_MDI
2510   wxMDIChildFrame *subframe = new wxMDIChildFrame (theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2511 #else
2512   wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2513 #endif
2514
2515   wxMenu *file_menu = new wxMenu;
2516   
2517   file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
2518   file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...");
2519   file_menu->Append(wxID_OPEN, "&Open...");
2520   file_menu->Append(wxID_SAVE, "&Save");
2521   file_menu->Append(wxID_SAVEAS, "Save &As...");
2522   file_menu->Append(wxID_CLOSE, "&Close");
2523   
2524   file_menu->AppendSeparator();
2525   file_menu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
2526   
2527   file_menu->AppendSeparator();
2528   file_menu->Append(wxID_PRINT, "&Print...");
2529   file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2530   file_menu->Append(wxID_PREVIEW, "Print Pre&view");
2531   
2532   wxMenu *view_menu = new wxMenu;
2533   view_menu->Append(PLOTMENU_VIEW_SCALE_MINMAX, "Display Scale &Set...");
2534   view_menu->Append(PLOTMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...");
2535   view_menu->Append(PLOTMENU_VIEW_SCALE_FULL, "Display &Full Scale");
2536   
2537   wxMenu *help_menu = new wxMenu;
2538   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
2539   help_menu->AppendSeparator();
2540   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2541   
2542   wxMenuBar *menu_bar = new wxMenuBar;
2543   
2544   menu_bar->Append(file_menu, "&File");
2545   menu_bar->Append(view_menu, "&View");
2546   menu_bar->Append(help_menu, "&Help");
2547   
2548   subframe->SetMenuBar(menu_bar);
2549   
2550   subframe->Centre(wxBOTH);
2551   
2552   return subframe;
2553 }
2554
2555
2556 bool 
2557 PlotFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
2558 {
2559   m_frame = CreateChildFrame(doc, this);
2560   SetFrame(m_frame);
2561   
2562   m_bMinSpecified = false;
2563   m_bMaxSpecified = false;
2564   m_dAutoScaleFactor = 1.;
2565   
2566   int width, height;
2567   m_frame->GetClientSize(&width, &height);
2568   m_frame->SetTitle ("Plot File");
2569   m_canvas = CreateCanvas (this, m_frame);
2570   
2571 #ifdef __X__
2572   int x, y;  // X requires a forced resize
2573   m_frame->GetSize(&x, &y);
2574   m_frame->SetSize(-1, -1, x, y);
2575 #endif
2576   
2577   m_frame->Show(true);
2578   Activate(true);
2579    
2580   return true;
2581 }
2582
2583 void 
2584 PlotFileView::OnDraw (wxDC* dc)
2585 {
2586   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2587   const int iNColumns = rPlotFile.getNumColumns();
2588   const int iNRecords = rPlotFile.getNumRecords();
2589   
2590   if (iNColumns > 0 && iNRecords > 0) {
2591     int xsize, ysize;
2592     m_canvas->GetClientSize (&xsize, &ysize);
2593     SGPDriver driver (dc, xsize, ysize);
2594     SGP sgp (driver);
2595     if (m_pEZPlot)
2596       m_pEZPlot->plot (&sgp);
2597   }
2598 }
2599
2600
2601 void 
2602 PlotFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2603 {
2604     const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2605     const int iNColumns = rPlotFile.getNumColumns();
2606     const int iNRecords = rPlotFile.getNumRecords();
2607     
2608     if (iNColumns > 0 && iNRecords > 0) {
2609       if (m_pEZPlot)
2610         delete m_pEZPlot;
2611       m_pEZPlot = new EZPlot;
2612       
2613       for (unsigned int iEzset = 0; iEzset < rPlotFile.getNumEzsetCommands(); iEzset++)
2614         m_pEZPlot->ezset (rPlotFile.getEzsetCommand (iEzset));
2615       
2616       if (m_bMinSpecified) {
2617         std::ostringstream os;
2618         os << "ymin " << m_dMinPixel;
2619         m_pEZPlot->ezset (os.str());
2620       }
2621       
2622       if (m_bMaxSpecified) {
2623         std::ostringstream os;
2624         os << "ymax " << m_dMaxPixel;
2625         m_pEZPlot->ezset (os.str());
2626       }
2627       
2628       m_pEZPlot->ezset("box");
2629       m_pEZPlot->ezset("grid");
2630       
2631       double* pdXaxis = new double [iNRecords];
2632       rPlotFile.getColumn (0, pdXaxis);
2633       
2634       double* pdY = new double [iNRecords];
2635       for (int iCol = 1; iCol < iNColumns; iCol++) {
2636         rPlotFile.getColumn (iCol, pdY);
2637         m_pEZPlot->addCurve (pdXaxis, pdY, iNRecords);
2638       }
2639       
2640       delete pdXaxis;
2641       delete pdY;
2642     }
2643
2644     if (m_canvas)
2645       m_canvas->Refresh();
2646 }
2647
2648 bool 
2649 PlotFileView::OnClose (bool deleteWindow)
2650 {
2651   if (!GetDocument()->Close())
2652     return false;
2653   
2654   // m_canvas->Clear();
2655   m_canvas->m_pView = NULL;
2656   m_canvas = NULL;
2657   wxString s(wxTheApp->GetAppName());
2658   if (m_frame)
2659     m_frame->SetTitle(s);
2660   SetFrame(NULL);
2661   
2662   Activate(false);
2663   
2664   if (deleteWindow) {
2665     delete m_frame;
2666     return true;
2667   }
2668   return true;
2669 }
2670