WPILibC++  2020.3.2-60-g3011ebe
cscore_raw.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef CSCORE_CSCORE_RAW_H_
9 #define CSCORE_CSCORE_RAW_H_
10 
11 #include "cscore_c.h"
12 
13 #ifdef __cplusplus
14 #include "cscore_oo.h"
15 #endif
16 
20 typedef struct CS_RawFrame {
21  char* data;
22  int dataLength;
23  int pixelFormat;
24  int width;
25  int height;
26  int totalData;
27 } CS_RawFrame;
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
37 void CS_AllocateRawFrameData(CS_RawFrame* frame, int requestedSize);
38 void CS_FreeRawFrameData(CS_RawFrame* frame);
39 
40 uint64_t CS_GrabRawSinkFrame(CS_Sink sink, struct CS_RawFrame* rawImage,
41  CS_Status* status);
42 uint64_t CS_GrabRawSinkFrameTimeout(CS_Sink sink, struct CS_RawFrame* rawImage,
43  double timeout, CS_Status* status);
44 
45 CS_Sink CS_CreateRawSink(const char* name, CS_Status* status);
46 
47 CS_Sink CS_CreateRawSinkCallback(const char* name, void* data,
48  void (*processFrame)(void* data,
49  uint64_t time),
50  CS_Status* status);
51 
52 void CS_PutRawSourceFrame(CS_Source source, const struct CS_RawFrame* image,
53  CS_Status* status);
54 
55 CS_Source CS_CreateRawSource(const char* name, const CS_VideoMode* mode,
56  CS_Status* status);
59 #ifdef __cplusplus
60 } // extern "C"
61 #endif
62 
63 #ifdef __cplusplus
64 namespace cs {
65 
66 struct RawFrame : public CS_RawFrame {
67  RawFrame() {
68  data = nullptr;
69  dataLength = 0;
70  pixelFormat = CS_PIXFMT_UNKNOWN;
71  width = 0;
72  height = 0;
73  totalData = 0;
74  }
75 
76  ~RawFrame() { CS_FreeRawFrameData(this); }
77 };
78 
84 CS_Source CreateRawSource(const wpi::Twine& name, const VideoMode& mode,
85  CS_Status* status);
86 
87 CS_Sink CreateRawSink(const wpi::Twine& name, CS_Status* status);
88 CS_Sink CreateRawSinkCallback(const wpi::Twine& name,
89  std::function<void(uint64_t time)> processFrame,
90  CS_Status* status);
91 
92 void PutSourceFrame(CS_Source source, const CS_RawFrame& image,
93  CS_Status* status);
94 uint64_t GrabSinkFrame(CS_Sink sink, CS_RawFrame& image, CS_Status* status);
95 uint64_t GrabSinkFrameTimeout(CS_Sink sink, CS_RawFrame& image, double timeout,
96  CS_Status* status);
97 
103 class RawSource : public ImageSource {
104  public:
105  RawSource() = default;
106 
113  RawSource(const wpi::Twine& name, const VideoMode& mode);
114 
124  RawSource(const wpi::Twine& name, VideoMode::PixelFormat pixelFormat,
125  int width, int height, int fps);
126 
127  protected:
133  void PutFrame(RawFrame& image);
134 };
135 
141 class RawSink : public ImageSink {
142  public:
143  RawSink() = default;
144 
153  explicit RawSink(const wpi::Twine& name);
154 
167  RawSink(const wpi::Twine& name,
168  std::function<void(uint64_t time)> processFrame);
169 
170  protected:
180  uint64_t GrabFrame(RawFrame& image, double timeout = 0.225) const;
181 
190  uint64_t GrabFrameNoTimeout(RawFrame& image) const;
191 };
192 
193 inline RawSource::RawSource(const wpi::Twine& name, const VideoMode& mode) {
194  m_handle = CreateRawSource(name, mode, &m_status);
195 }
196 
197 inline RawSource::RawSource(const wpi::Twine& name,
198  VideoMode::PixelFormat format, int width,
199  int height, int fps) {
200  m_handle =
201  CreateRawSource(name, VideoMode{format, width, height, fps}, &m_status);
202 }
203 
204 inline void RawSource::PutFrame(RawFrame& image) {
205  m_status = 0;
206  PutSourceFrame(m_handle, image, &m_status);
207 }
208 
209 inline RawSink::RawSink(const wpi::Twine& name) {
210  m_handle = CreateRawSink(name, &m_status);
211 }
212 
213 inline RawSink::RawSink(const wpi::Twine& name,
214  std::function<void(uint64_t time)> processFrame) {
215  m_handle = CreateRawSinkCallback(name, processFrame, &m_status);
216 }
217 
218 inline uint64_t RawSink::GrabFrame(RawFrame& image, double timeout) const {
219  m_status = 0;
220  return GrabSinkFrameTimeout(m_handle, image, timeout, &m_status);
221 }
222 
223 inline uint64_t RawSink::GrabFrameNoTimeout(RawFrame& image) const {
224  m_status = 0;
225  return GrabSinkFrame(m_handle, image, &m_status);
226 }
227 
228 } // namespace cs
229 
232 #endif
233 
234 #endif // CSCORE_CSCORE_RAW_H_
wpi::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:132
CS_VideoMode
Video mode.
Definition: cscore_c.h:104
cs
CameraServer (cscore) namespace.
Definition: cscore_raw_cv.h:17
CS_RawFrame
Raw Frame.
Definition: cscore_raw.h:20
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85