ChannelUtilities.hh
Go to the documentation of this file.
1 
35 #ifndef CHANNEL_UTILITIES_HH
36 #define CHANNEL_UTILITIES_HH
37 
38 #include <array>
39 #include <memory>
40 
41 #include <MultiSense/MultiSenseChannel.hh>
42 
43 using namespace crl::multisense;
44 
45 //
46 // Wrapper around a Channel pointer to make cleanup easier
47 
49 {
50  public:
51 
52  ChannelWrapper(const std::string& ipAddress) :
53  channelPtr_(Channel::Create(ipAddress))
54  {
55  }
56 
58  {
59  if (channelPtr_) {
60  Channel::Destroy(channelPtr_);
61  }
62  }
63 
64  Channel* ptr() noexcept
65  {
66  return channelPtr_;
67  }
68 
69  private:
70 
71  ChannelWrapper(const ChannelWrapper&) = delete;
72  ChannelWrapper operator=(const ChannelWrapper&) = delete;
73 
74  Channel* channelPtr_ = nullptr;
75 };
76 
77 //
78 // Wrapper to preserve image data outside of the image callback
79 
81 {
82  public:
84  const crl::multisense::image::Header& data) :
85  driver_(driver),
86  callbackBuffer_(driver->reserveCallbackBuffer()),
87  data_(data)
88  {
89  }
90 
92  {
93  if (driver_) {
94  driver_->releaseCallbackBuffer(callbackBuffer_);
95  }
96  }
97 
98  const image::Header& data() const noexcept
99  {
100  return data_;
101  }
102 
103  private:
104 
105  ImageBufferWrapper(const ImageBufferWrapper&) = delete;
106  ImageBufferWrapper operator=(const ImageBufferWrapper&) = delete;
107 
108  crl::multisense::Channel* driver_ = nullptr;
111 
112 };
113 
114 template<typename T>
115 constexpr std::array<uint8_t, 3> ycbcrToBgr(const crl::multisense::image::Header& luma,
116  const crl::multisense::image::Header& chroma,
117  const size_t u,
118  const size_t v)
119 {
120  const uint8_t* lumaP = reinterpret_cast<const uint8_t*>(luma.imageDataP);
121  const uint8_t* chromaP = reinterpret_cast<const uint8_t*>(chroma.imageDataP);
122 
123  const size_t luma_offset = (v * luma.width) + u;
124  const size_t chroma_offset = 2 * (((v / 2) * (luma.width / 2)) + (u / 2));
125 
126  const float px_y = static_cast<float>(lumaP[luma_offset]);
127  const float px_cb = static_cast<float>(chromaP[chroma_offset + 0]) - 128.0f;
128  const float px_cr = static_cast<float>(chromaP[chroma_offset + 1]) - 128.0f;
129 
130  float px_r = px_y + 1.13983f * px_cr;
131  float px_g = px_y - 0.39465f * px_cb - 0.58060f * px_cr;
132  float px_b = px_y + 2.03211f * px_cb;
133 
134  if (px_r < 0.0f) px_r = 0.0f;
135  else if (px_r > 255.0f) px_r = 255.0f;
136  if (px_g < 0.0f) px_g = 0.0f;
137  else if (px_g > 255.0f) px_g = 255.0f;
138  if (px_b < 0.0f) px_b = 0.0f;
139  else if (px_b > 255.0f) px_b = 255.0f;
140 
141  return { {static_cast<uint8_t>(px_r), static_cast<uint8_t>(px_g), static_cast<uint8_t>(px_b)} };
142 }
143 
145  const crl::multisense::image::Header& chroma,
146  uint8_t* output)
147 {
148  if (luma.bitsPerPixel != 8 || chroma.bitsPerPixel != 16)
149  {
150  throw std::runtime_error("Only 8-bit luma and 16-bit chroma images are supported by the \
151  ycbcrToBgr conversion function");
152  }
153 
154  const size_t rgb_stride = luma.width * 3;
155 
156  for (uint32_t y = 0; y < luma.height; ++y)
157  {
158  const size_t row_offset = y * rgb_stride;
159 
160  for (uint32_t x = 0; x < luma.width; ++x)
161  {
162  memcpy(output + row_offset + (3 * x), ycbcrToBgr<uint8_t>(luma, chroma, x, y).data(), 3);
163  }
164  }
165 }
166 
167 #endif //CHANNEL_UTILITIES_HH
168 
crl::multisense::image::Header::height
uint32_t height
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:470
ImageBufferWrapper
Definition: ChannelUtilities.hh:80
crl::multisense::Channel::Destroy
static void Destroy(Channel *instanceP)
Definition: Legacy/details/channel.cc:863
crl::multisense::image::Header::width
uint32_t width
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:468
f
f
ImageBufferWrapper::callbackBuffer_
void * callbackBuffer_
Definition: ChannelUtilities.hh:109
ImageBufferWrapper::data_
const image::Header data_
Definition: ChannelUtilities.hh:110
ycbcrToBgr
constexpr std::array< uint8_t, 3 > ycbcrToBgr(const crl::multisense::image::Header &luma, const crl::multisense::image::Header &chroma, const size_t u, const size_t v)
Definition: ChannelUtilities.hh:115
crl::multisense::image::Header::bitsPerPixel
uint32_t bitsPerPixel
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:466
crl::multisense::Channel
Definition: Legacy/include/MultiSense/MultiSenseChannel.hh:69
ImageBufferWrapper::~ImageBufferWrapper
~ImageBufferWrapper()
Definition: ChannelUtilities.hh:91
ImageBufferWrapper::data
const image::Header & data() const noexcept
Definition: ChannelUtilities.hh:98
crl::multisense
Definition: Legacy/details/channel.cc:62
crl::multisense::image::Header::imageDataP
const void * imageDataP
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:487
ChannelWrapper::ptr
Channel * ptr() noexcept
Definition: ChannelUtilities.hh:64
ChannelWrapper
Definition: ChannelUtilities.hh:48
ChannelWrapper::~ChannelWrapper
~ChannelWrapper()
Definition: ChannelUtilities.hh:57
ChannelWrapper::ChannelWrapper
ChannelWrapper(const std::string &ipAddress)
Definition: ChannelUtilities.hh:52
ImageBufferWrapper::ImageBufferWrapper
ImageBufferWrapper(crl::multisense::Channel *driver, const crl::multisense::image::Header &data)
Definition: ChannelUtilities.hh:83
crl::multisense::image::Header
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:460


multisense_lib
Author(s):
autogenerated on Thu Apr 17 2025 02:49:08