WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
cscore_raw.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef CSCORE_CSCORE_RAW_H_
6#define CSCORE_CSCORE_RAW_H_
7
8#include <functional>
9
10#include "cscore_c.h"
11
12// NOLINTBEGIN
13#ifdef __cplusplus
14#include "cscore_oo.h"
15#endif
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20// NOLINTEND
21
22/**
23 * @defgroup cscore_raw_cfunc Raw Image Functions
24 * @{
25 */
26uint64_t CS_GrabRawSinkFrame(CS_Sink sink, struct WPI_RawFrame* rawImage,
27 CS_Status* status);
28uint64_t CS_GrabRawSinkFrameTimeout(CS_Sink sink, struct WPI_RawFrame* rawImage,
29 double timeout, CS_Status* status);
31 struct WPI_RawFrame* rawImage,
32 double timeout,
33 uint64_t lastFrameTime,
34 CS_Status* status);
35
36CS_Sink CS_CreateRawSink(const struct WPI_String* name, CS_Bool isCv,
37 CS_Status* status);
38
40 const struct WPI_String* name, CS_Bool isCv, void* data,
41 void (*processFrame)(void* data, uint64_t time), CS_Status* status);
42
44 CS_Status* status);
45
47 const CS_VideoMode* mode, CS_Status* status);
48/** @} */
49
50#ifdef __cplusplus
51} // extern "C"
52#endif
53
54#ifdef __cplusplus
55namespace cs {
56
57/**
58 * @defgroup cscore_raw_func Raw Image Functions
59 * @{
60 */
61
62CS_Source CreateRawSource(std::string_view name, bool isCv,
63 const VideoMode& mode, CS_Status* status);
64
65CS_Sink CreateRawSink(std::string_view name, bool isCv, CS_Status* status);
66CS_Sink CreateRawSinkCallback(std::string_view name, bool isCv,
67 std::function<void(uint64_t time)> processFrame,
68 CS_Status* status);
69
71 CS_Status* status);
72uint64_t GrabSinkFrame(CS_Sink sink, WPI_RawFrame& image, CS_Status* status);
73uint64_t GrabSinkFrameTimeout(CS_Sink sink, WPI_RawFrame& image, double timeout,
74 CS_Status* status);
76 double timeout, uint64_t lastFrameTime,
77 CS_Status* status);
78
79/**
80 * A source for user code to provide video frames as raw bytes.
81 *
82 * This is a complex API, most cases should use CvSource.
83 */
84class RawSource : public ImageSource {
85 public:
86 RawSource() = default;
87
88 /**
89 * Create a raw frame source.
90 *
91 * @param name Source name (arbitrary unique identifier)
92 * @param mode Video mode being generated
93 */
94 RawSource(std::string_view name, const VideoMode& mode);
95
96 /**
97 * Create a raw frame source.
98 *
99 * @param name Source name (arbitrary unique identifier)
100 * @param pixelFormat Pixel format
101 * @param width width
102 * @param height height
103 * @param fps fps
104 */
105 RawSource(std::string_view name, VideoMode::PixelFormat pixelFormat,
106 int width, int height, int fps);
107
108 protected:
109 /**
110 * Put a raw image and notify sinks.
111 *
112 * @param image raw frame image
113 */
114 void PutFrame(wpi::RawFrame& image);
115};
116
117/**
118 * A sink for user code to accept video frames as raw bytes.
119 *
120 * This is a complex API, most cases should use CvSource.
121 */
122class RawSink : public ImageSink {
123 public:
124 RawSink() = default;
125
126 /**
127 * Create a sink for accepting raw images.
128 *
129 * <p>GrabFrame() must be called on the created sink to get each new
130 * image.
131 *
132 * @param name Source name (arbitrary unique identifier)
133 */
134 explicit RawSink(std::string_view name);
135
136 /**
137 * Create a sink for accepting raws images in a separate thread.
138 *
139 * <p>A thread will be created that calls WaitForFrame() and calls the
140 * processFrame() callback each time a new frame arrives.
141 *
142 * @param name Source name (arbitrary unique identifier)
143 * @param processFrame Frame processing function; will be called with a
144 * time=0 if an error occurred. processFrame should call GetImage()
145 * or GetError() as needed, but should not call (except in very
146 * unusual circumstances) WaitForImage().
147 */
148 RawSink(std::string_view name,
149 std::function<void(uint64_t time)> processFrame);
150
151 protected:
152 /**
153 * Wait for the next frame and get the image.
154 * Times out (returning 0) after timeout seconds.
155 * The provided image will have three 8-bit channels stored in BGR order.
156 *
157 * @return Frame time, or 0 on error (call GetError() to obtain the error
158 * message); the frame time is in the same time base as wpi::Now(),
159 * and is in 1 us increments.
160 */
161 [[nodiscard]]
162 uint64_t GrabFrame(wpi::RawFrame& image, double timeout = 0.225) const;
163
164 /**
165 * Wait for the next frame and get the image. May block forever.
166 * The provided image will have three 8-bit channels stored in BGR order.
167 *
168 * @return Frame time, or 0 on error (call GetError() to obtain the error
169 * message); the frame time is in the same time base as wpi::Now(),
170 * and is in 1 us increments.
171 */
172 [[nodiscard]]
173 uint64_t GrabFrameNoTimeout(wpi::RawFrame& image) const;
174
175 /**
176 * Wait for the next frame and get the image. May block forever.
177 * The provided image will have three 8-bit channels stored in BGR order.
178 *
179 * <p>If lastFrameTime is provided and non-zero, the sink will fill image with
180 * the first frame from the source that is not equal to lastFrameTime. If
181 * lastFrameTime is zero, the time of the current frame owned by the CvSource
182 * is used, and this function will block until the connected CvSource provides
183 * a new frame.
184 *
185 * @return Frame time, or 0 on error (call GetError() to obtain the error
186 * message); the frame time is in the same time base as wpi::Now(),
187 * and is in 1 us increments.
188 */
189 [[nodiscard]]
190 uint64_t GrabFrameLastTime(wpi::RawFrame& image, uint64_t lastFrameTime,
191 double timeout = 0.225) const;
192};
193
194inline RawSource::RawSource(std::string_view name, const VideoMode& mode) {
195 m_handle = CreateRawSource(name, false, mode, &m_status);
196}
197
198inline RawSource::RawSource(std::string_view name,
200 int height, int fps) {
201 m_handle = CreateRawSource(name, false, VideoMode{format, width, height, fps},
202 &m_status);
203}
204
206 m_status = 0;
208}
209
210inline RawSink::RawSink(std::string_view name) {
211 m_handle = CreateRawSink(name, false, &m_status);
212}
213
214inline RawSink::RawSink(std::string_view name,
215 std::function<void(uint64_t time)> processFrame) {
216 m_handle = CreateRawSinkCallback(name, false, processFrame, &m_status);
217}
218
219inline uint64_t RawSink::GrabFrame(wpi::RawFrame& image, double timeout) const {
220 m_status = 0;
221 return GrabSinkFrameTimeout(m_handle, image, timeout, &m_status);
222}
223
224inline uint64_t RawSink::GrabFrameNoTimeout(wpi::RawFrame& image) const {
225 m_status = 0;
226 return GrabSinkFrame(m_handle, image, &m_status);
227}
228
230 uint64_t lastFrameTime,
231 double timeout) const {
232 m_status = 0;
233 return GrabSinkFrameTimeoutLastTime(m_handle, image, timeout, lastFrameTime,
234 &m_status);
235}
236/** @} */
237
238} // namespace cs
239
240#endif
241
242#endif // CSCORE_CSCORE_RAW_H_
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or and nanopb were all modified for use in Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation source
Definition ThirdPartyNotices.txt:124
A base class for single image reading sinks.
Definition cscore_oo.h:1451
A base class for single image providing sources.
Definition cscore_oo.h:984
A sink for user code to accept video frames as raw bytes.
Definition cscore_raw.h:122
RawSink()=default
A source for user code to provide video frames as raw bytes.
Definition cscore_raw.h:84
RawSource()=default
CS_Status m_status
Definition cscore_oo.h:1339
CS_Sink m_handle
Definition cscore_oo.h:1340
CS_Source m_handle
Video source handle.
Definition cscore_oo.h:603
CS_Status m_status
Definition cscore_oo.h:600
FMT_INLINE auto format(detail::locale_ref loc, format_string< T... > fmt, T &&... args) -> std::string
Definition format.h:4146
uint64_t CS_GrabRawSinkFrameTimeout(CS_Sink sink, struct WPI_RawFrame *rawImage, double timeout, CS_Status *status)
CS_Source CS_CreateRawSource(const struct WPI_String *name, CS_Bool isCv, const CS_VideoMode *mode, CS_Status *status)
CS_Sink CS_CreateRawSink(const struct WPI_String *name, CS_Bool isCv, CS_Status *status)
CS_Sink CS_CreateRawSinkCallback(const struct WPI_String *name, CS_Bool isCv, void *data, void(*processFrame)(void *data, uint64_t time), CS_Status *status)
void CS_PutRawSourceFrame(CS_Source source, const struct WPI_RawFrame *image, CS_Status *status)
uint64_t CS_GrabRawSinkFrameTimeoutWithFrameTime(CS_Sink sink, struct WPI_RawFrame *rawImage, double timeout, uint64_t lastFrameTime, CS_Status *status)
uint64_t CS_GrabRawSinkFrame(CS_Sink sink, struct WPI_RawFrame *rawImage, CS_Status *status)
uint64_t GrabSinkFrameTimeoutLastTime(CS_Sink sink, WPI_RawFrame &image, double timeout, uint64_t lastFrameTime, CS_Status *status)
uint64_t GrabFrame(wpi::RawFrame &image, double timeout=0.225) const
Wait for the next frame and get the image.
Definition cscore_raw.h:219
CS_Source CreateRawSource(std::string_view name, bool isCv, const VideoMode &mode, CS_Status *status)
CS_Sink CreateRawSink(std::string_view name, bool isCv, CS_Status *status)
uint64_t GrabSinkFrameTimeout(CS_Sink sink, WPI_RawFrame &image, double timeout, CS_Status *status)
uint64_t GrabSinkFrame(CS_Sink sink, WPI_RawFrame &image, CS_Status *status)
void PutSourceFrame(CS_Source source, const WPI_RawFrame &image, CS_Status *status)
void PutFrame(wpi::RawFrame &image)
Put a raw image and notify sinks.
Definition cscore_raw.h:205
uint64_t GrabFrameNoTimeout(wpi::RawFrame &image) const
Wait for the next frame and get the image.
Definition cscore_raw.h:224
CS_Sink CreateRawSinkCallback(std::string_view name, bool isCv, std::function< void(uint64_t time)> processFrame, CS_Status *status)
uint64_t GrabFrameLastTime(wpi::RawFrame &image, uint64_t lastFrameTime, double timeout=0.225) const
Wait for the next frame and get the image.
Definition cscore_raw.h:229
CS_Handle CS_Source
Definition cscore_c.h:54
int CS_Status
Definition cscore_c.h:47
CS_Handle CS_Sink
Definition cscore_c.h:53
int CS_Bool
Definition cscore_c.h:46
CameraServer (cscore) namespace.
Definition ShuffleboardContainer.h:25
Video mode.
Definition cscore_c.h:93
Raw Frame.
Definition RawFrame.h:32
A const UTF8 string.
Definition string.h:14
Video mode.
Definition cscore_cpp.h:62
PixelFormat
Definition cscore_cpp.h:63
Definition RawFrame.h:92