Fawkes API  Fawkes Development Version
scaled_viewer.cpp
1 
2 /***************************************************************************
3  * scale_viewer.cpp - Generic scale viewer tool
4  *
5  * Created: Thu Aug 25 16:13:34 2011 (based on fvviewer)
6  * Copyright 2005-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 
24 #include <core/exceptions/software.h>
25 #include <utils/system/argparser.h>
26 #include <utils/time/tracker.h>
27 
28 #include <fvcams/factory.h>
29 #ifdef HAVE_SHMEM_CAM
30 #include <fvcams/shmem.h>
31 #endif
32 #ifdef HAVE_NETWORK_CAM
33 #include <fvcams/net.h>
34 #endif
35 #ifdef HAVE_FILELOADER_CAM
36 #include <fvcams/fileloader.h>
37 #endif
38 
39 #include <cstring>
40 #include <cstdio>
41 #include <stdint.h>
42 
43 #include <gtkmm.h>
44 
45 #include <fvutils/color/conversions.h>
46 
47 using namespace fawkes;
48 using namespace firevision;
49 
50 Gtk::Image *img_image;
51 Camera *cam;
52 
53 TimeTracker tt;
54 int ttc_capture;
55 int ttc_convert;
56 int ttc_draw;
57 int ttc_interloop;
58 unsigned int loop_count = 0;
59 
60 static bool
61 timeout_handler()
62 {
63  tt.ping_end(ttc_interloop);
64 
65  tt.ping_start(ttc_capture);
66  cam->capture();
67  tt.ping_end(ttc_capture);
68 
69  tt.ping_start(ttc_convert);
70  unsigned int orig_width = cam->pixel_width();
71  unsigned int orig_height = cam->pixel_height();
72 
73  unsigned char *rgb_buffer = malloc_buffer(RGB, orig_width, orig_height);
74 
75  convert(cam->colorspace(), RGB, cam->buffer(), rgb_buffer,
76  orig_width, orig_height);
77  tt.ping_end(ttc_convert);
78 
79  tt.ping_start(ttc_draw);
80  Glib::RefPtr<Gdk::Pixbuf> image =
81  Gdk::Pixbuf::create_from_data( rgb_buffer, Gdk::COLORSPACE_RGB, false,
82  8, orig_width, orig_height, 3 * orig_width);
83 
84  int width = img_image->get_width();
85  int height = img_image->get_height();
86  Glib::RefPtr<Gdk::Pixbuf> scaled = image->scale_simple(width, height,
87  Gdk::INTERP_NEAREST);
88 
89  img_image->set(scaled);
90  img_image->queue_draw();
91 
92  tt.ping_end(ttc_draw);
93 
94  cam->dispose_buffer();
95 
96  free(rgb_buffer);
97 
98  if (++loop_count >= 10) {
99  loop_count = 0;
100  tt.print_to_stdout();
101  }
102 
103  tt.ping_start(ttc_interloop);
104  return true;
105 }
106 
107 
108 
109 void
110 print_usage(const char *program_name)
111 {
112  printf("Usage: %s -n host[:port]/image_id [-j] [-d delay] [-v]\n\n"
113  " -n net_string Open network camera, the camera string is of the form\n"
114  " host[:port]/image_id. You have to specify at least the host\n"
115  " and the image_id, the port is optional and defaults to 5000\n"
116  " -j Receive JPEG images, only valid with -n\n"
117  " -d delay Delay in ms before a new image is capture.\n",
118  program_name);
119 }
120 
121 
122 
123 int
124 main(int argc, char **argv)
125 {
126  ArgumentParser argp(argc, argv, "hn:jd:");
127 
128  Gtk::Main gtk_main(argc, argv);
129 
130  //bool verbose = argp.has_arg("v");
131  int delay = 300;
132 
133  if ( argp.has_arg("d") ) {
134  delay = atoi(argp.arg("d"));
135  if ( delay < 0 ) delay = 300;
136  }
137 
138  if ( argp.has_arg("h") ) {
139  print_usage(argp.program_name());
140  exit(0);
141  } else if ( argp.has_arg("n") ) {
142  char *net_string = strdup(argp.arg("n"));
143  char *image_id = NULL, *host = NULL, *port = NULL, *save_ptr = NULL;
144  int port_num = 2208;
145  char *hostport;
146 
147  hostport = strtok_r(net_string, "/", &save_ptr);
148  image_id = strtok_r(NULL, "", &save_ptr);
149 
150  if ( strchr(hostport, ':') != NULL ) {
151  host = strtok_r(hostport, ":", &save_ptr);
152  port = strtok_r(NULL, "", &save_ptr);
153  } else {
154  host = hostport;
155  }
156 
157  if ( port != NULL ) {
158  port_num = atoi(port);
159  if ( (port_num < 0) || (port_num > 0xFFFF) ) {
160  throw OutOfBoundsException("Invalid port", port_num, 0, 0xFFFF);
161  }
162  }
163 
164  if( image_id == NULL ) {
165  throw IllegalArgumentException("Image ID must be specified");
166  }
167 
168  cam = new NetworkCamera(host, port_num, image_id, argp.has_arg("j"));
169  free(net_string);
170  } else {
171  print_usage(argp.program_name());
172  exit(1);
173  }
174 
175  try {
176  cam->open();
177  cam->start();
178  } catch (Exception &e) {
179  printf("Failed to open camera\n");
180  e.print_trace();
181  delete cam;
182  exit(-2);
183  }
184 
185  ttc_capture = tt.add_class("Capture");
186  ttc_convert = tt.add_class("Convert");
187  ttc_draw = tt.add_class("Draw");
188  ttc_interloop = tt.add_class("InterLoop");
189  tt.ping_start(ttc_interloop);
190 
191  Glib::RefPtr<Gtk::Builder> builder;
192  builder =
193  Gtk::Builder::create_from_file(RESDIR"/guis/scale_viewer/scale_viewer.ui");
194 
195  Gtk::Window *window;
196 
197  builder->get_widget("wnd_main", window);
198  builder->get_widget("img_image", img_image);
199 
200  Glib::signal_timeout().connect(sigc::ptr_fun(&timeout_handler), delay);
201 
202  window->set_size_request(320, 240);
203  Gtk::Main::run(*window);
204 
205  cam->close();
206  delete cam;
207 
208  return 0;
209 }
210 
void ping_start(unsigned int cls)
Start of given class task.
Definition: tracker.cpp:228
Camera interface for image aquiring devices in FireVision.
Definition: camera.h:35
Fawkes library namespace.
virtual unsigned int pixel_width()=0
Width of image in pixels.
Parse command line arguments.
Definition: argparser.h:66
virtual colorspace_t colorspace()=0
Colorspace of returned image.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void capture()=0
Capture an image.
unsigned int add_class(std::string name)
Add a new class.
Definition: tracker.cpp:156
Time tracking utility.
Definition: tracker.h:38
void ping_end(unsigned int cls)
End of given class task.
Definition: tracker.cpp:254
virtual void open()=0
Open the camera.
void print_to_stdout()
Print results to stdout.
Definition: tracker.cpp:317
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
virtual void close()=0
Close camera.
virtual unsigned char * buffer()=0
Get access to current image buffer.
virtual unsigned int pixel_height()=0
Height of image in pixels.
virtual void start()=0
Start image transfer from the camera.
Network camera.
Definition: net.h:42
Index out of bounds.
Definition: software.h:88
Expected parameter is missing.
Definition: software.h:82
virtual void dispose_buffer()=0
Dispose current buffer.