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