blob: 9841ca28d179d6988178256d0e8e0741fd1c48be [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
Austin Schuh60e77942022-05-16 17:48:24 -07004#include <memory>
5
Parker Schuh6691f192017-01-14 17:01:02 -08006#include "aos/vision/blob/range_image.h"
Parker Schuhef47dbf2017-03-04 16:59:30 -08007#include "aos/vision/debug/debug_window.h"
Parker Schuh6691f192017-01-14 17:01:02 -08008#include "aos/vision/image/image_types.h"
9
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080010namespace aos::vision {
Parker Schuh6691f192017-01-14 17:01:02 -080011
Parker Schuhef47dbf2017-03-04 16:59:30 -080012class BlobStreamViewer : public DebugWindow {
Parker Schuh6691f192017-01-14 17:01:02 -080013 public:
Parker Schuhef47dbf2017-03-04 16:59:30 -080014 BlobStreamViewer() : DebugWindow(false) {}
15 explicit BlobStreamViewer(bool flip) : DebugWindow(flip) {}
Parker Schuhd7db83d2017-02-08 20:49:15 -080016
Parker Schuh6691f192017-01-14 17:01:02 -080017 void Submit(ImageFormat fmt, const BlobList &blob_list) {
18 SetFormatAndClear(fmt);
19 DrawBlobList(blob_list, {255, 255, 255});
20 }
21
22 inline void SetFormatAndClear(ImageFormat fmt) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080023 if (!image_.fmt().Equals(fmt)) {
Parker Schuh6691f192017-01-14 17:01:02 -080024 printf("resizing data: %d, %d\n", fmt.w, fmt.h);
Parker Schuhd7db83d2017-02-08 20:49:15 -080025 image_ = ImageValue(fmt);
26 UpdateImage(image_.get());
Parker Schuh6691f192017-01-14 17:01:02 -080027 }
Parker Schuhd7db83d2017-02-08 20:49:15 -080028 memset(image_.data(), 0, fmt.ImgSize() * sizeof(PixelRef));
Parker Schuh6691f192017-01-14 17:01:02 -080029 }
30
Parker Schuh309dd722017-02-25 11:31:18 -080031 template <typename PixelCallback>
32 inline void ForPxInBlobList(const BlobList &blob_list,
33 PixelCallback pixel_callback) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080034 ImagePtr ptr = img();
Parker Schuh309dd722017-02-25 11:31:18 -080035 auto fmt = ptr.fmt();
36 for (const auto &blob : blob_list) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080037 for (int i = 0; i < (int)blob.ranges().size(); ++i) {
Parker Schuh309dd722017-02-25 11:31:18 -080038 int y = blob.min_y() + i;
39 if (y >= 0 && y < fmt.h) {
40 for (const auto &range : blob.ranges()[i]) {
41 for (int j = std::max(0, range.st); j < std::min(fmt.w, range.ed);
42 ++j) {
43 pixel_callback(ptr.get_px(j, y));
44 }
Parker Schuh6691f192017-01-14 17:01:02 -080045 }
46 }
47 }
48 }
49 }
50
Parker Schuh309dd722017-02-25 11:31:18 -080051 inline void DrawBlobList(const BlobList &blob_list, PixelRef color) {
52 ForPxInBlobList(blob_list, [&](PixelRef &px) { px = color; });
53 }
54
Parker Schuh6691f192017-01-14 17:01:02 -080055 inline void DrawSecondBlobList(const BlobList &blob_list, PixelRef color1,
56 PixelRef color2) {
Parker Schuh309dd722017-02-25 11:31:18 -080057 ForPxInBlobList(blob_list, [&](PixelRef &px) {
58 if (px.r == 0 && px.g == 0 && px.b == 0) {
59 px = color1;
60 } else {
61 px = color2;
Parker Schuh6691f192017-01-14 17:01:02 -080062 }
Parker Schuh309dd722017-02-25 11:31:18 -080063 });
Parker Schuh6691f192017-01-14 17:01:02 -080064 }
65
Parker Schuh0ff777c2017-02-19 15:01:13 -080066 inline void DrawSecondBlobList(const BlobList &blob_list, PixelRef color1,
67 PixelRef color2, PixelRef prev_color) {
Parker Schuh309dd722017-02-25 11:31:18 -080068 ForPxInBlobList(blob_list, [&](PixelRef &px) {
69 if (px.r == prev_color.r && px.g == prev_color.g &&
70 px.b == prev_color.b) {
71 px = color2;
72 } else {
73 px = color1;
Parker Schuh0ff777c2017-02-19 15:01:13 -080074 }
Parker Schuh309dd722017-02-25 11:31:18 -080075 });
Parker Schuh0ff777c2017-02-19 15:01:13 -080076 }
77
Parker Schuhd7db83d2017-02-08 20:49:15 -080078 // Backwards compatible.
Parker Schuhef47dbf2017-03-04 16:59:30 -080079 DebugWindow *view() { return this; }
Parker Schuhd7db83d2017-02-08 20:49:15 -080080
81 ImagePtr img() { return image_.get(); }
Parker Schuh6691f192017-01-14 17:01:02 -080082
83 private:
Parker Schuhd7db83d2017-02-08 20:49:15 -080084 ImageValue image_;
Parker Schuh6691f192017-01-14 17:01:02 -080085};
86
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080087} // namespace aos::vision
Parker Schuh6691f192017-01-14 17:01:02 -080088
89#endif // _AOS_VISION_BLOB_STREAM_VIEW_H_