blob: 9d63e8d780082bdb1d3443ba2fc0dae31fd9c88f [file] [log] [blame]
Parker Schuh6691f192017-01-14 17:01:02 -08001#ifndef _AOS_VISION_BLOB_STREAM_VIEW_H_
2#define _AOS_VISION_BLOB_STREAM_VIEW_H_
3
4#include "aos/vision/blob/range_image.h"
5#include "aos/vision/debug/debug_viewer.h"
6#include "aos/vision/image/image_types.h"
7
8#include <memory>
9
10namespace aos {
11namespace vision {
12
13class BlobStreamViewer {
14 public:
15 BlobStreamViewer() : view_(false) {}
16 void Submit(ImageFormat fmt, const BlobList &blob_list) {
17 SetFormatAndClear(fmt);
18 DrawBlobList(blob_list, {255, 255, 255});
19 }
20
21 inline void SetFormatAndClear(ImageFormat fmt) {
22 if (fmt.w != ptr_.fmt().w || fmt.h != ptr_.fmt().h) {
23 printf("resizing data: %d, %d\n", fmt.w, fmt.h);
24 outbuf_.reset(new PixelRef[fmt.ImgSize()]);
25 ptr_ = ImagePtr{fmt, outbuf_.get()};
26 view_.UpdateImage(ptr_);
27 }
28 memset(ptr_.data(), 0, fmt.ImgSize() * sizeof(PixelRef));
29 }
30
31 inline void DrawBlobList(const BlobList &blob_list, PixelRef color) {
32 for (const auto &blob : blob_list) {
33 for (int i = 0; i < (int)blob.ranges.size(); ++i) {
34 for (const auto &range : blob.ranges[i]) {
35 for (int j = range.st; j < range.ed; ++j) {
36 ptr_.get_px(j, i + blob.min_y) = color;
37 }
38 }
39 }
40 }
41 }
42
43 inline void DrawSecondBlobList(const BlobList &blob_list, PixelRef color1,
44 PixelRef color2) {
45 for (const auto &blob : blob_list) {
46 for (int i = 0; i < (int)blob.ranges.size(); ++i) {
47 for (const auto &range : blob.ranges[i]) {
48 for (int j = range.st; j < range.ed; ++j) {
49 auto px = ptr_.get_px(j, i + blob.min_y);
50 if (px.r == 0 && px.g == 0 && px.b == 0) {
51 ptr_.get_px(j, i + blob.min_y) = color1;
52 } else {
53 ptr_.get_px(j, i + blob.min_y) = color2;
54 }
55 }
56 }
57 }
58 }
59 }
60
61 DebugViewer *view() { return &view_; }
62
63 private:
64 std::unique_ptr<PixelRef[]> outbuf_;
65 ImagePtr ptr_;
66
67 DebugViewer view_;
68};
69
70} // namespace vision
71} // namespace aos
72
73#endif // _AOS_VISION_BLOB_STREAM_VIEW_H_