blob: 99dfd8b465a5930c0d4d19f95ebc685969863b4d [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
5#include "aos/common/logging/implementations.h"
6#include "aos/common/logging/logging.h"
7#include "aos/vision/blob/find_blob.h"
8#include "aos/vision/blob/stream_view.h"
9#include "aos/vision/debug/debug_viewer.h"
10#include "aos/vision/events/epoll_events.h"
11#include "aos/vision/image/jpeg_routines.h"
12
13namespace aos {
14namespace vision {
15
16bool DecodeJpeg(aos::vision::DataRef data,
17 aos::vision::BlobStreamViewer *view) {
18 auto fmt = aos::vision::GetFmt(data);
19 auto value = view->img();
20 if (!value.fmt().Equals(fmt)) {
21 view->SetFormatAndClear(fmt);
22 }
23 return aos::vision::ProcessJpeg(data, view->img().data());
24}
25
26class DebugFramework : public DebugFrameworkInterface {
27 public:
28 explicit DebugFramework(FilterHarness *filter) : filter_(filter) {
29 view_.key_press_event = [this](uint32_t keyval) {
30 for (const auto &event : key_press_events()) {
31 event(keyval);
32 }
33 };
34 filter->InstallViewer(&view_);
35 }
36
37 // This the first stage in the pipeline that takes
38 void NewJpeg(DataRef data) override {
39 DecodeJpeg(data, &view_);
40
41 auto fmt = view_.img().fmt();
42 HandleBlobs(FindBlobs(filter_->Threshold(view_.img())), fmt);
43 }
44
45 void NewBlobList(BlobList blob_list, ImageFormat fmt) override {
46 view_.SetFormatAndClear(fmt);
47
48 HandleBlobs(std::move(blob_list), fmt);
49 }
50
51 void HandleBlobs(BlobList blob_list, ImageFormat fmt) {
52 filter_->HandleBlobs(std::move(blob_list), fmt);
53 view_.Redraw();
54 }
55
56 aos::events::EpollLoop *Loop() override { return &loop_; }
57
58 private:
59 FilterHarness *filter_;
60 BlobStreamViewer view_;
61
62 aos::events::EpollLoop loop_;
63};
64
65std::unique_ptr<ImageSource> MakeImageSource(
66 const std::string &image_source_string,
67 DebugFrameworkInterface *interface) {
68 (void)interface;
69 // Each of the image_source strings is of the form format_type:format_spec
70 auto it = image_source_string.find(':');
71 if (it == std::string::npos) {
72 fprintf(stderr, "invalid ImageSource: %s.\n", image_source_string.c_str());
73 exit(-1);
74 }
75 auto image_source_type = image_source_string.substr(0, it);
76 // Get std::function<std::unique_ptr<ImageSource>()> from the registration
77 // factory.
78 const auto &factory = ImageSourceGlobalFactory::Get(image_source_type);
79 if (!factory) {
80 fprintf(stderr, "invalid ImageSource: %s.\n", image_source_string.c_str());
81 exit(-1);
82 }
83 auto result = factory();
84 // Construct the image source.
85 result->Init(image_source_string.substr(it + 1), interface);
86 return result;
87}
88
89const char *kHelpMessage = R"(
90
91image_source is parsed out and selects where to get the images
92from. Each source type has a different configuration format string listed
93below. The colon separates the source specifier and the source config
94parameter. A single command line argument help will print this message.
95)";
96
97void DebugFrameworkMain(int argc, char **argv, FilterHarness *filter) {
98 ::aos::logging::Init();
99 ::aos::logging::AddImplementation(
100 new ::aos::logging::StreamLogImplementation(stdout));
101
102 gtk_init(&argc, &argv);
103
104 // Use fprintf because it is only supposed to be used interactively.
105 // This uses a registration system to pick out the individual file type
106 // registered by REGISTER_IMAGE_SOURCE.
107 // see jpeg_list-source.cc for ane sample of this.
108 if (argc < 2 || argv[1] == std::string("help")) {
109 fprintf(stderr, "Usage %s image_source:format_spec\n", argv[0]);
110 fprintf(stderr, "%s", kHelpMessage);
111 // Iterate through all registered entities in ImageSourceGlobalFactory
112 // and print out their individual help messages.
113 for (const auto &type : ImageSourceGlobalFactory::GetAll()) {
114 fprintf(stderr, " %s:\n", type.first.c_str());
115 fprintf(stderr, "%s", type.second()->GetHelpMessage());
116 }
117 exit(-1);
118 }
119
120 DebugFramework replay(filter);
121
122 std::unique_ptr<ImageSource> image_source = MakeImageSource(argv[1], &replay);
123
124 replay.Loop()->RunWithGtkMain();
125}
126
127} // namespace vision
128} // namespace aos