blob: 0b0d2c79b28da2d24d80caf6a0fabd4ae2963c3a [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) {
Parker Schuh02f13f62019-02-16 16:42:41 -080061 view_.SetScale(1.0);
Parker Schuhcd258b82017-04-09 16:28:29 -070062 }
Alex Perry5b1e8e32019-04-07 13:25:31 -070063
64 // Pass along the set exposure so that users can acceess it.
Austin Schuha0c41ba2020-09-10 22:59:14 -070065 filter->InstallSetExposure(
66 [this](uint32_t abs_exp) { this->SetExposure(abs_exp); });
Parker Schuh90641112017-02-25 12:18:36 -080067 }
68
69 // This the first stage in the pipeline that takes
Parker Schuhcd258b82017-04-09 16:28:29 -070070 bool NewJpeg(DataRef data) override {
Parker Schuh90641112017-02-25 12:18:36 -080071 DecodeJpeg(data, &view_);
72
73 auto fmt = view_.img().fmt();
Parker Schuhcd258b82017-04-09 16:28:29 -070074 return HandleBlobs(FindBlobs(filter_->Threshold(view_.img())), fmt);
Parker Schuh90641112017-02-25 12:18:36 -080075 }
76
Parker Schuh02f13f62019-02-16 16:42:41 -080077 bool NewImage(ImageFormat fmt,
78 const std::function<bool(ImagePtr data)> &process) override {
79 auto value = view_.img();
80 if (!value.fmt().Equals(fmt)) {
81 view_.SetFormatAndClear(fmt);
82 }
83 process(view_.img());
84
85 return HandleBlobs(FindBlobs(filter_->Threshold(view_.img())), fmt);
86 }
87
Parker Schuhcd258b82017-04-09 16:28:29 -070088 bool NewBlobList(BlobList blob_list, ImageFormat fmt) override {
Parker Schuh90641112017-02-25 12:18:36 -080089 view_.SetFormatAndClear(fmt);
90
Parker Schuhcd258b82017-04-09 16:28:29 -070091 return HandleBlobs(std::move(blob_list), fmt);
Parker Schuh90641112017-02-25 12:18:36 -080092 }
93
Parker Schuhcd258b82017-04-09 16:28:29 -070094 bool JustCheckForTarget(BlobList blob_list, ImageFormat fmt) override {
95 return filter_->JustCheckForTarget(std::move(blob_list), fmt);
96 }
97
98 bool HandleBlobs(BlobList blob_list, ImageFormat fmt) {
99 bool result = filter_->HandleBlobs(std::move(blob_list), fmt);
Parker Schuh90641112017-02-25 12:18:36 -0800100 view_.Redraw();
Parker Schuhcd258b82017-04-09 16:28:29 -0700101 return result;
Parker Schuh90641112017-02-25 12:18:36 -0800102 }
103
104 aos::events::EpollLoop *Loop() override { return &loop_; }
105
Parker Schuhcd258b82017-04-09 16:28:29 -0700106 const CameraParams &camera_params() override { return camera_params_; }
107
108 BlobStreamViewer *viewer() override { return &view_; }
109
Parker Schuh90641112017-02-25 12:18:36 -0800110 private:
Parker Schuhcd258b82017-04-09 16:28:29 -0700111 CameraParams camera_params_;
Parker Schuh90641112017-02-25 12:18:36 -0800112 FilterHarness *filter_;
113 BlobStreamViewer view_;
114
115 aos::events::EpollLoop loop_;
116};
117
118std::unique_ptr<ImageSource> MakeImageSource(
119 const std::string &image_source_string,
120 DebugFrameworkInterface *interface) {
121 (void)interface;
122 // Each of the image_source strings is of the form format_type:format_spec
123 auto it = image_source_string.find(':');
124 if (it == std::string::npos) {
125 fprintf(stderr, "invalid ImageSource: %s.\n", image_source_string.c_str());
126 exit(-1);
127 }
128 auto image_source_type = image_source_string.substr(0, it);
129 // Get std::function<std::unique_ptr<ImageSource>()> from the registration
130 // factory.
131 const auto &factory = ImageSourceGlobalFactory::Get(image_source_type);
132 if (!factory) {
133 fprintf(stderr, "invalid ImageSource: %s.\n", image_source_string.c_str());
134 exit(-1);
135 }
136 auto result = factory();
137 // Construct the image source.
138 result->Init(image_source_string.substr(it + 1), interface);
139 return result;
140}
141
142const char *kHelpMessage = R"(
143
144image_source is parsed out and selects where to get the images
145from. Each source type has a different configuration format string listed
146below. The colon separates the source specifier and the source config
147parameter. A single command line argument help will print this message.
148)";
149
Parker Schuhcd258b82017-04-09 16:28:29 -0700150void DebugFrameworkMain(int argc, char **argv, FilterHarness *filter,
151 CameraParams camera_params) {
Parker Schuh90641112017-02-25 12:18:36 -0800152 ::aos::logging::Init();
Parker Schuh90641112017-02-25 12:18:36 -0800153
154 gtk_init(&argc, &argv);
155
156 // Use fprintf because it is only supposed to be used interactively.
157 // This uses a registration system to pick out the individual file type
158 // registered by REGISTER_IMAGE_SOURCE.
159 // see jpeg_list-source.cc for ane sample of this.
160 if (argc < 2 || argv[1] == std::string("help")) {
161 fprintf(stderr, "Usage %s image_source:format_spec\n", argv[0]);
162 fprintf(stderr, "%s", kHelpMessage);
163 // Iterate through all registered entities in ImageSourceGlobalFactory
164 // and print out their individual help messages.
165 for (const auto &type : ImageSourceGlobalFactory::GetAll()) {
166 fprintf(stderr, " %s:\n", type.first.c_str());
167 fprintf(stderr, "%s", type.second()->GetHelpMessage());
168 }
169 exit(-1);
170 }
171
Parker Schuhcd258b82017-04-09 16:28:29 -0700172 DebugFramework replay(filter, camera_params);
Parker Schuh90641112017-02-25 12:18:36 -0800173
174 std::unique_ptr<ImageSource> image_source = MakeImageSource(argv[1], &replay);
175
176 replay.Loop()->RunWithGtkMain();
177}
178
179} // namespace vision
180} // namespace aos