Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleImageCanvasRectangularSelection.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Vision N GmbH
5  Schauenburgerstr. 116
6  24118 Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
21 
22 
23 /**
24  @example ExampleImageCanvas.cpp
25  @relates ImageCanvasRectangularSelection
26  @brief Simple example for ImageCanvasRectangularSelection usage
27  with callback Function
28  @ingroup g_examples
29  @author MIP
30 */
31 
32 
33 #include <Base/Common/W32Compat.hh>
34 
35 #include <wx/wx.h>
36 
37 #include <Gui/ImageCanvasRectangularSelection.hh>
38 #include <Base/Image/ImageIO.hh>
39 #include <Gui/StringConv.hh>
40 using namespace BIAS;
41 using namespace std;
42 
43 enum { ID_Quit = 1, ID_About, ID_Load, ID_Rect};
44 /** \cond HIDDEN_SYMBOLS */
45 class MyApp: public wxApp
46 {
47  virtual bool OnInit();
48 };
49 
50 IMPLEMENT_APP(MyApp)
51 
52 class MyFrame: public wxFrame, public ICECallbackInterface
53 {
54 public:
55 
56  MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
57 
58  void OnQuit(wxCommandEvent& event);
59  void OnAbout(wxCommandEvent& event);
60  void OnLoad(wxCommandEvent& event);
61  void OnRect(wxCommandEvent& event);
62 
63  virtual void RectangleSelected(int tl[2], int br[2]);
64 
66 
67  DECLARE_EVENT_TABLE()
68 };
69 
70 
71 bool MyApp::OnInit()
72 {
73  MyFrame *frame = new MyFrame( AsciiToWx("ImageCanvasRectangularSelection"), wxPoint(50,50),
74  wxSize(450,340) );
75  frame->Show(TRUE);
76  SetTopWindow(frame);
77  return TRUE;
78 }
79 
80 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
81  EVT_MENU(ID_Quit, MyFrame::OnQuit)
82  EVT_MENU(ID_About, MyFrame::OnAbout)
83  EVT_MENU(ID_Load, MyFrame::OnLoad)
84  EVT_MENU(ID_Rect, MyFrame::OnRect)
85 END_EVENT_TABLE()
86 
87 
88 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
89  : wxFrame((wxFrame *)NULL, -1, title, pos, size)
90 {
91  wxMenu *menuFile = new wxMenu;
92 
93  menuFile->Append( ID_About, AsciiToWx("&About...") );
94  menuFile->Append( ID_Load, AsciiToWx("&Load...") );
95  menuFile->Append( ID_Rect, AsciiToWx("&Rect...") );
96  menuFile->AppendSeparator();
97  menuFile->Append( ID_Quit, AsciiToWx("E&xit") );
98 
99  wxMenuBar *menuBar = new wxMenuBar;
100  menuBar->Append(menuFile, AsciiToWx("&File") );
101  SetMenuBar(menuBar);
102 
103  CreateStatusBar(2);
104  SetStatusText(AsciiToWx("ImageName"), 0);
105 
106  wxBoxSizer *box=new wxBoxSizer(wxVERTICAL);
107  _ic=new ImageCanvasRectangularSelection(this, GetStatusBar(), 1);
108  box->Add(_ic, 1, wxALL| wxGROW |wxALIGN_CENTER, 5);
109  box->SetSizeHints(this);
110  SetSizer(box);
111 
112  //const string fn("ucim.pgm");
113  const string fn(BIAS_TESTS_DATA "r4.jpg" );
115  int res=ImageIO::Load(fn, im);
116  if (res!=0){
117  cout<<"could not load "<<fn<<endl;
118  wxMessageBox(_T( "could not load image. aborting. ") );
119  BIASABORT;
120  }
121  SetStatusText(_T("image"), 0);
122  _ic->Show(im, "image" );
123  FitInside();
124 }
125 
126 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
127 { Close(TRUE); }
128 
129 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
130 {
131  wxMessageBox(AsciiToWx("This is a wxWidgtets Hello world sample"),
132  AsciiToWx("About Hello World")
133  , wxOK | wxICON_INFORMATION, this);
134 }
135 
136 void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event))
137 {
138  wxFileDialog fileDialog(this, AsciiToWx("Choose a file"), AsciiToWx("."),
139  AsciiToWx("ucim.pgm"));
140  if (fileDialog.ShowModal() == wxID_OK){
141  string fname=WxToAscii(fileDialog.GetPath());
143  if (ImageIO::Load(fname, im)!=0){
144  BIASERR("error loading "<<fname);
145  } else {
146  SetStatusText(fileDialog.GetFilename(), 0);
147  _ic->Show(im, WxToAscii(fileDialog.GetFilename()));
148  }
149  } else {
150  cerr << "canceled loadin image" << endl;
151  }
152 }
153 
154 void MyFrame::OnRect(wxCommandEvent& WXUNUSED(event))
155 {
156  _ic->SelectRectangle(*this);
157 }
158 
159 void MyFrame::RectangleSelected(int tl[2], int br[2])
160 {
161  cerr << "finished select rectangle: ("<<tl[0]<<", "<<tl[1]<<") <-> ("
162  <<br[0]<<", "<<br[1]<<")\n";
163 }
164 /** \endcond */
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
provides functionality for selection of rectangle, point, ...
callback interface for ImageCanvasExtended
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141