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