blob: a6a661a5e27f125c1fd397f345dc89284f489494 [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
Parker Schuhd7db83d2017-02-08 20:49:15 -080013class BlobStreamViewer : public DebugViewer {
Parker Schuh6691f192017-01-14 17:01:02 -080014 public:
Parker Schuhd7db83d2017-02-08 20:49:15 -080015 BlobStreamViewer() : DebugViewer(false) {}
16 explicit BlobStreamViewer(bool flip) : DebugViewer(flip) {}
17
Parker Schuh6691f192017-01-14 17:01:02 -080018 void Submit(ImageFormat fmt, const BlobList &blob_list) {
19 SetFormatAndClear(fmt);
20 DrawBlobList(blob_list, {255, 255, 255});
21 }
22
23 inline void SetFormatAndClear(ImageFormat fmt) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080024 if (!image_.fmt().Equals(fmt)) {
Parker Schuh6691f192017-01-14 17:01:02 -080025 printf("resizing data: %d, %d\n", fmt.w, fmt.h);
Parker Schuhd7db83d2017-02-08 20:49:15 -080026 image_ = ImageValue(fmt);
27 UpdateImage(image_.get());
Parker Schuh6691f192017-01-14 17:01:02 -080028 }
Parker Schuhd7db83d2017-02-08 20:49:15 -080029 memset(image_.data(), 0, fmt.ImgSize() * sizeof(PixelRef));
Parker Schuh6691f192017-01-14 17:01:02 -080030 }
31
Parker Schuh309dd722017-02-25 11:31:18 -080032 template <typename PixelCallback>
33 inline void ForPxInBlobList(const BlobList &blob_list,
34 PixelCallback pixel_callback) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080035 ImagePtr ptr = img();
Parker Schuh309dd722017-02-25 11:31:18 -080036 auto fmt = ptr.fmt();
37 for (const auto &blob : blob_list) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080038 for (int i = 0; i < (int)blob.ranges().size(); ++i) {
Parker Schuh309dd722017-02-25 11:31:18 -080039 int y = blob.min_y() + i;
40 if (y >= 0 && y < fmt.h) {
41 for (const auto &range : blob.ranges()[i]) {
42 for (int j = std::max(0, range.st); j < std::min(fmt.w, range.ed);
43 ++j) {
44 pixel_callback(ptr.get_px(j, y));
45 }
Parker Schuh6691f192017-01-14 17:01:02 -080046 }
47 }
48 }
49 }
50 }
51
Parker Schuh309dd722017-02-25 11:31:18 -080052 inline void DrawBlobList(const BlobList &blob_list, PixelRef color) {
53 ForPxInBlobList(blob_list, [&](PixelRef &px) { px = color; });
54 }
55
Parker Schuh6691f192017-01-14 17:01:02 -080056 inline void DrawSecondBlobList(const BlobList &blob_list, PixelRef color1,
57 PixelRef color2) {
Parker Schuh309dd722017-02-25 11:31:18 -080058 ForPxInBlobList(blob_list, [&](PixelRef &px) {
59 if (px.r == 0 && px.g == 0 && px.b == 0) {
60 px = color1;
61 } else {
62 px = color2;
Parker Schuh6691f192017-01-14 17:01:02 -080063 }
Parker Schuh309dd722017-02-25 11:31:18 -080064 });
Parker Schuh6691f192017-01-14 17:01:02 -080065 }
66
Parker Schuh0ff777c2017-02-19 15:01:13 -080067 inline void DrawSecondBlobList(const BlobList &blob_list, PixelRef color1,
68 PixelRef color2, PixelRef prev_color) {
Parker Schuh309dd722017-02-25 11:31:18 -080069 ForPxInBlobList(blob_list, [&](PixelRef &px) {
70 if (px.r == prev_color.r && px.g == prev_color.g &&
71 px.b == prev_color.b) {
72 px = color2;
73 } else {
74 px = color1;
Parker Schuh0ff777c2017-02-19 15:01:13 -080075 }
Parker Schuh309dd722017-02-25 11:31:18 -080076 });
Parker Schuh0ff777c2017-02-19 15:01:13 -080077 }
78
Parker Schuhd7db83d2017-02-08 20:49:15 -080079 // Backwards compatible.
80 DebugViewer *view() { return this; }
81
82 ImagePtr img() { return image_.get(); }
Parker Schuh6691f192017-01-14 17:01:02 -080083
84 private:
Parker Schuhd7db83d2017-02-08 20:49:15 -080085 ImageValue image_;
Parker Schuh6691f192017-01-14 17:01:02 -080086};
87
88} // namespace vision
89} // namespace aos
90
91#endif // _AOS_VISION_BLOB_STREAM_VIEW_H_