blob: 46f5400c2894ef2947291cab28b8eb06d4ee7d29 [file] [log] [blame]
Parker Schuh90641112017-02-25 12:18:36 -08001#include "aos/vision/debug/debug_framework.h"
2
3#include <gtk/gtk.h>
4
John Park33858a32018-09-28 23:05:48 -07005#include "aos/logging/implementations.h"
6#include "aos/logging/logging.h"
Parker Schuh90641112017-02-25 12:18:36 -08007#include "aos/vision/blob/find_blob.h"
8#include "aos/vision/blob/stream_view.h"
Parker Schuh90641112017-02-25 12:18:36 -08009#include "aos/vision/events/epoll_events.h"
10#include "aos/vision/image/jpeg_routines.h"
11
12namespace aos {
13namespace vision {
14
Parker Schuhcd258b82017-04-09 16:28:29 -070015// Detect screen height on smaller monitors.
16int GetScreenHeight() {
17 fprintf(stderr, "gtk version_info: %d.%d.%d\n", gtk_get_major_version(),
18 gtk_get_minor_version(), gtk_get_micro_version());
19
20 GdkScreen *screen = gdk_screen_get_default();
21 GdkRectangle dimensions;
22// Deprecated in newer versions of GTK and missing from older versions.
23#if GTK_CHECK_VERSION(3, 22, 7)
24 GdkDisplay *display = gdk_screen_get_display(screen);
25 GdkMonitor *monitor = gdk_display_get_primary_monitor(display);
26 gdk_monitor_get_geometry(monitor, &dimensions);
27#else
28 dimensions.height = gdk_screen_get_height(screen);
29 dimensions.width = gdk_screen_get_width(screen);
30#endif
31 fprintf(stdout, "Monitor dimensions: %dx%d\n", dimensions.width,
32 dimensions.height);
33 return dimensions.height;
34}
35
Parker Schuh90641112017-02-25 12:18:36 -080036bool DecodeJpeg(aos::vision::DataRef data,
37 aos::vision::BlobStreamViewer *view) {
38 auto fmt = aos::vision::GetFmt(data);
39 auto value = view->img();
40 if (!value.fmt().Equals(fmt)) {
41 view->SetFormatAndClear(fmt);
42 }
43 return aos::vision::ProcessJpeg(data, view->img().data());
44}
45
46class DebugFramework : public DebugFrameworkInterface {
47 public:
Parker Schuhcd258b82017-04-09 16:28:29 -070048 explicit DebugFramework(FilterHarness *filter, CameraParams camera_params)
49 : camera_params_(camera_params), filter_(filter) {
Parker Schuh90641112017-02-25 12:18:36 -080050 view_.key_press_event = [this](uint32_t keyval) {
51 for (const auto &event : key_press_events()) {
52 event(keyval);
53 }
54 };
55 filter->InstallViewer(&view_);
Parker Schuhcd258b82017-04-09 16:28:29 -070056 auto key_press = filter->RegisterKeyPress();
57 if (key_press) {
58 InstallKeyPress(key_press);
59 }
60 if (GetScreenHeight() < 1024) {
61 view_.SetScale(0.75);
62 }
Parker Schuh90641112017-02-25 12:18:36 -080063 }
64
65 // This the first stage in the pipeline that takes
Parker Schuhcd258b82017-04-09 16:28:29 -070066 bool NewJpeg(DataRef data) override {
Parker Schuh90641112017-02-25 12:18:36 -080067 DecodeJpeg(data, &view_);
68
69 auto fmt = view_.img().fmt();
Parker Schuhcd258b82017-04-09 16:28:29 -070070 return HandleBlobs(FindBlobs(filter_->Threshold(view_.img())), fmt);
Parker Schuh90641112017-02-25 12:18:36 -080071 }
72
Parker Schuhcd258b82017-04-09 16:28:29 -070073 bool NewBlobList(BlobList blob_list, ImageFormat fmt) override {
Parker Schuh90641112017-02-25 12:18:36 -080074 view_.SetFormatAndClear(fmt);
75
Parker Schuhcd258b82017-04-09 16:28:29 -070076 return HandleBlobs(std::move(blob_list), fmt);
Parker Schuh90641112017-02-25 12:18:36 -080077 }
78
Parker Schuhcd258b82017-04-09 16:28:29 -070079 bool JustCheckForTarget(BlobList blob_list, ImageFormat fmt) override {
80 return filter_->JustCheckForTarget(std::move(blob_list), fmt);
81 }
82
83 bool HandleBlobs(BlobList blob_list, ImageFormat fmt) {
84 bool result = filter_->HandleBlobs(std::move(blob_list), fmt);
Parker Schuh90641112017-02-25 12:18:36 -080085 view_.Redraw();
Parker Schuhcd258b82017-04-09 16:28:29 -070086 return result;
Parker Schuh90641112017-02-25 12:18:36 -080087 }
88
89 aos::events::EpollLoop *Loop() override { return &loop_; }
90
Parker Schuhcd258b82017-04-09 16:28:29 -070091 const CameraParams &camera_params() override { return camera_params_; }
92
93 BlobStreamViewer *viewer() override { return &view_; }
94
Parker Schuh90641112017-02-25 12:18:36 -080095 private:
Parker Schuhcd258b82017-04-09 16:28:29 -070096 CameraParams camera_params_;
Parker Schuh90641112017-02-25 12:18:36 -080097 FilterHarness *filter_;
98 BlobStreamViewer view_;
99
100 aos::events::EpollLoop loop_;
101};
102
103std::unique_ptr<ImageSource> MakeImageSource(
104 const std::string &image_source_string,
105 DebugFrameworkInterface *interface) {
106 (void)interface;
107 // Each of the image_source strings is of the form format_type:format_spec
108 auto it = image_source_string.find(':');
109 if (it == std::string::npos) {
110 fprintf(stderr, "invalid ImageSource: %s.\n", image_source_string.c_str());
111 exit(-1);
112 }
113 auto image_source_type = image_source_string.substr(0, it);
114 // Get std::function<std::unique_ptr<ImageSource>()> from the registration
115 // factory.
116 const auto &factory = ImageSourceGlobalFactory::Get(image_source_type);
117 if (!factory) {
118 fprintf(stderr, "invalid ImageSource: %s.\n", image_source_string.c_str());
119 exit(-1);
120 }
121 auto result = factory();
122 // Construct the image source.
123 result->Init(image_source_string.substr(it + 1), interface);
124 return result;
125}
126
127const char *kHelpMessage = R"(
128
129image_source is parsed out and selects where to get the images
130from. Each source type has a different configuration format string listed
131below. The colon separates the source specifier and the source config
132parameter. A single command line argument help will print this message.
133)";
134
Parker Schuhcd258b82017-04-09 16:28:29 -0700135void DebugFrameworkMain(int argc, char **argv, FilterHarness *filter,
136 CameraParams camera_params) {
Parker Schuh90641112017-02-25 12:18:36 -0800137 ::aos::logging::Init();
138 ::aos::logging::AddImplementation(
139 new ::aos::logging::StreamLogImplementation(stdout));
140
141 gtk_init(&argc, &argv);
142
143 // Use fprintf because it is only supposed to be used interactively.
144 // This uses a registration system to pick out the individual file type
145 // registered by REGISTER_IMAGE_SOURCE.
146 // see jpeg_list-source.cc for ane sample of this.
147 if (argc < 2 || argv[1] == std::string("help")) {
148 fprintf(stderr, "Usage %s image_source:format_spec\n", argv[0]);
149 fprintf(stderr, "%s", kHelpMessage);
150 // Iterate through all registered entities in ImageSourceGlobalFactory
151 // and print out their individual help messages.
152 for (const auto &type : ImageSourceGlobalFactory::GetAll()) {
153 fprintf(stderr, " %s:\n", type.first.c_str());
154 fprintf(stderr, "%s", type.second()->GetHelpMessage());
155 }
156 exit(-1);
157 }
158
Parker Schuhcd258b82017-04-09 16:28:29 -0700159 DebugFramework replay(filter, camera_params);
Parker Schuh90641112017-02-25 12:18:36 -0800160
161 std::unique_ptr<ImageSource> image_source = MakeImageSource(argv[1], &replay);
162
163 replay.Loop()->RunWithGtkMain();
164}
165
166} // namespace vision
167} // namespace aos