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 #include <core/exceptions/software.h>
24 #include <fvcams/factory.h>
25 #include <utils/system/argparser.h>
26 #include <utils/time/tracker.h>
27 #ifdef HAVE_SHMEM_CAM
28 # include <fvcams/shmem.h>
29 #endif
30 #ifdef HAVE_NETWORK_CAM
31 # include <fvcams/net.h>
32 #endif
33 #ifdef HAVE_FILELOADER_CAM
34 # include <fvcams/fileloader.h>
35 #endif
36 
37 #include <fvutils/color/conversions.h>
38 
39 #include <cstdio>
40 #include <cstring>
41 #include <gtkmm.h>
42 #include <stdint.h>
43 
44 using namespace fawkes;
45 using namespace firevision;
46 
47 Gtk::Image *img_image;
48 Camera * cam;
49 
50 TimeTracker tt;
51 int ttc_capture;
52 int ttc_convert;
53 int ttc_draw;
54 int ttc_interloop;
55 unsigned int loop_count = 0;
56 
57 static bool
58 timeout_handler()
59 {
60  tt.ping_end(ttc_interloop);
61 
62  tt.ping_start(ttc_capture);
63  cam->capture();
64  tt.ping_end(ttc_capture);
65 
66  tt.ping_start(ttc_convert);
67  unsigned int orig_width = cam->pixel_width();
68  unsigned int orig_height = cam->pixel_height();
69 
70  unsigned char *rgb_buffer = malloc_buffer(RGB, orig_width, orig_height);
71 
72  convert(cam->colorspace(), RGB, cam->buffer(), rgb_buffer, orig_width, orig_height);
73  tt.ping_end(ttc_convert);
74 
75  tt.ping_start(ttc_draw);
76  Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_data(
77  rgb_buffer, Gdk::COLORSPACE_RGB, false, 8, orig_width, orig_height, 3 * orig_width);
78 
79  int width = img_image->get_width();
80  int height = img_image->get_height();
81  Glib::RefPtr<Gdk::Pixbuf> scaled = image->scale_simple(width, height, Gdk::INTERP_NEAREST);
82 
83  img_image->set(scaled);
84  img_image->queue_draw();
85 
86  tt.ping_end(ttc_draw);
87 
88  cam->dispose_buffer();
89 
90  free(rgb_buffer);
91 
92  if (++loop_count >= 10) {
93  loop_count = 0;
94  tt.print_to_stdout();
95  }
96 
97  tt.ping_start(ttc_interloop);
98  return true;
99 }
100 
101 void
102 print_usage(const char *program_name)
103 {
104  printf("Usage: %s -n host[:port]/image_id [-j] [-d delay] [-v]\n\n"
105  " -n net_string Open network camera, the camera string is of the form\n"
106  " host[:port]/image_id. You have to specify at least the host\n"
107  " and the image_id, the port is optional and defaults to 5000\n"
108  " -j Receive JPEG images, only valid with -n\n"
109  " -d delay Delay in ms before a new image is capture.\n",
110  program_name);
111 }
112 
113 int
114 main(int argc, char **argv)
115 {
116  ArgumentParser argp(argc, argv, "hn:jd:");
117 
118  Gtk::Main gtk_main(argc, argv);
119 
120  //bool verbose = argp.has_arg("v");
121  int delay = 300;
122 
123  if (argp.has_arg("d")) {
124  delay = atoi(argp.arg("d"));
125  if (delay < 0)
126  delay = 300;
127  }
128 
129  if (argp.has_arg("h")) {
130  print_usage(argp.program_name());
131  exit(0);
132  } else if (argp.has_arg("n")) {
133  char *net_string = strdup(argp.arg("n"));
134  char *image_id;
135  char *host = NULL;
136  char *port = NULL;
137  char *save_ptr = NULL;
138  int port_num = 2208;
139  char *hostport;
140 
141  hostport = strtok_r(net_string, "/", &save_ptr);
142  image_id = strtok_r(NULL, "", &save_ptr);
143 
144  if (strchr(hostport, ':') != NULL) {
145  host = strtok_r(hostport, ":", &save_ptr);
146  port = strtok_r(NULL, "", &save_ptr);
147  } else {
148  host = hostport;
149  }
150 
151  if (port != NULL) {
152  port_num = atoi(port);
153  if ((port_num < 0) || (port_num > 0xFFFF)) {
154  throw OutOfBoundsException("Invalid port", port_num, 0, 0xFFFF);
155  }
156  }
157 
158  if (image_id == NULL) {
159  throw IllegalArgumentException("Image ID must be specified");
160  }
161 
162  cam = new NetworkCamera(host, port_num, image_id, argp.has_arg("j"));
163  free(net_string);
164  } else {
165  print_usage(argp.program_name());
166  exit(1);
167  }
168 
169  try {
170  cam->open();
171  cam->start();
172  } catch (Exception &e) {
173  printf("Failed to open camera\n");
174  e.print_trace();
175  delete cam;
176  exit(-2);
177  }
178 
179  ttc_capture = tt.add_class("Capture");
180  ttc_convert = tt.add_class("Convert");
181  ttc_draw = tt.add_class("Draw");
182  ttc_interloop = tt.add_class("InterLoop");
183  tt.ping_start(ttc_interloop);
184 
185  Glib::RefPtr<Gtk::Builder> builder;
186  builder = Gtk::Builder::create_from_file(RESDIR "/guis/scale_viewer/scale_viewer.ui");
187 
188  Gtk::Window *window;
189 
190  builder->get_widget("wnd_main", window);
191  builder->get_widget("img_image", img_image);
192 
193  Glib::signal_timeout().connect(sigc::ptr_fun(&timeout_handler), delay);
194 
195  window->set_size_request(320, 240);
196  Gtk::Main::run(*window);
197 
198  cam->close();
199  delete cam;
200 
201  return 0;
202 }
fawkes::IllegalArgumentException
Definition: software.h:85
firevision::Camera::start
virtual void start()=0
firevision::NetworkCamera
Definition: net.h:46
fawkes::TimeTracker::ping_start
void ping_start(unsigned int cls)
Start of given class task.
Definition: tracker.cpp:218
firevision::Camera::colorspace
virtual colorspace_t colorspace()=0
fawkes::TimeTracker::print_to_stdout
void print_to_stdout()
Print results to stdout.
Definition: tracker.cpp:307
firevision::Camera::open
virtual void open()=0
firevision::Camera::dispose_buffer
virtual void dispose_buffer()=0
firevision::Camera::close
virtual void close()=0
fawkes::OutOfBoundsException
Definition: software.h:91
firevision::Camera::buffer
virtual unsigned char * buffer()=0
fawkes::TimeTracker::add_class
unsigned int add_class(std::string name)
Add a new class.
Definition: tracker.cpp:149
fawkes
firevision::Camera::pixel_height
virtual unsigned int pixel_height()=0
fawkes::ArgumentParser
Definition: argparser.h:69
fawkes::Exception::print_trace
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:601
firevision::Camera::pixel_width
virtual unsigned int pixel_width()=0
fawkes::TimeTracker
Definition: tracker.h:42
firevision::Camera::capture
virtual void capture()=0
firevision::Camera
Definition: camera.h:38
fawkes::TimeTracker::ping_end
void ping_end(unsigned int cls)
End of given class task.
Definition: tracker.cpp:243
fawkes::Exception
Definition: exception.h:41