WPILibC++ 2025.0.0-alpha-1-24-g6478ba6
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);
30
32 CS_Status* status);
33
35 const struct WPI_String* name, CS_Bool isCv, void* data,
36 void (*processFrame)(void* data, uint64_t time), CS_Status* status);
37
39 CS_Status* status);
40
42 const CS_VideoMode* mode, CS_Status* status);
43/** @} */
44
45#ifdef __cplusplus
46} // extern "C"
47#endif
48
49#ifdef __cplusplus
50namespace cs {
51
52/**
53 * @defgroup cscore_raw_func Raw Image Functions
54 * @{
55 */
56
58 const VideoMode& mode, CS_Status* status);
59
62 std::function<void(uint64_t time)> processFrame,
63 CS_Status* status);
64
66 CS_Status* status);
67uint64_t GrabSinkFrame(CS_Sink sink, WPI_RawFrame& image, CS_Status* status);
68uint64_t GrabSinkFrameTimeout(CS_Sink sink, WPI_RawFrame& image, double timeout,
69 CS_Status* status);
70
71/**
72 * A source for user code to provide video frames as raw bytes.
73 *
74 * This is a complex API, most cases should use CvSource.
75 */
76class RawSource : public ImageSource {
77 public:
78 RawSource() = default;
79
80 /**
81 * Create a raw frame source.
82 *
83 * @param name Source name (arbitrary unique identifier)
84 * @param mode Video mode being generated
85 */
86 RawSource(std::string_view name, const VideoMode& mode);
87
88 /**
89 * Create a raw frame source.
90 *
91 * @param name Source name (arbitrary unique identifier)
92 * @param pixelFormat Pixel format
93 * @param width width
94 * @param height height
95 * @param fps fps
96 */
98 int width, int height, int fps);
99
100 protected:
101 /**
102 * Put a raw image and notify sinks.
103 *
104 * @param image raw frame image
105 */
106 void PutFrame(wpi::RawFrame& image);
107};
108
109/**
110 * A sink for user code to accept video frames as raw bytes.
111 *
112 * This is a complex API, most cases should use CvSource.
113 */
114class RawSink : public ImageSink {
115 public:
116 RawSink() = default;
117
118 /**
119 * Create a sink for accepting raw images.
120 *
121 * <p>GrabFrame() must be called on the created sink to get each new
122 * image.
123 *
124 * @param name Source name (arbitrary unique identifier)
125 */
126 explicit RawSink(std::string_view name);
127
128 /**
129 * Create a sink for accepting raws images in a separate thread.
130 *
131 * <p>A thread will be created that calls WaitForFrame() and calls the
132 * processFrame() callback each time a new frame arrives.
133 *
134 * @param name Source name (arbitrary unique identifier)
135 * @param processFrame Frame processing function; will be called with a
136 * time=0 if an error occurred. processFrame should call GetImage()
137 * or GetError() as needed, but should not call (except in very
138 * unusual circumstances) WaitForImage().
139 */
141 std::function<void(uint64_t time)> processFrame);
142
143 protected:
144 /**
145 * Wait for the next frame and get the image.
146 * Times out (returning 0) after timeout seconds.
147 * The provided image will have three 8-bit channels stored in BGR order.
148 *
149 * @return Frame time, or 0 on error (call GetError() to obtain the error
150 * message); the frame time is in the same time base as wpi::Now(),
151 * and is in 1 us increments.
152 */
153 [[nodiscard]]
154 uint64_t GrabFrame(wpi::RawFrame& image, double timeout = 0.225) const;
155
156 /**
157 * Wait for the next frame and get the image. May block forever.
158 * The provided image will have three 8-bit channels stored in BGR order.
159 *
160 * @return Frame time, or 0 on error (call GetError() to obtain the error
161 * message); the frame time is in the same time base as wpi::Now(),
162 * and is in 1 us increments.
163 */
164 [[nodiscard]]
165 uint64_t GrabFrameNoTimeout(wpi::RawFrame& image) const;
166};
167
169 m_handle = CreateRawSource(name, false, mode, &m_status);
170}
171
174 int height, int fps) {
175 m_handle = CreateRawSource(name, false, VideoMode{format, width, height, fps},
176 &m_status);
177}
178
180 m_status = 0;
182}
183
185 m_handle = CreateRawSink(name, false, &m_status);
186}
187
189 std::function<void(uint64_t time)> processFrame) {
190 m_handle = CreateRawSinkCallback(name, false, processFrame, &m_status);
191}
192
193inline uint64_t RawSink::GrabFrame(wpi::RawFrame& image, double timeout) const {
194 m_status = 0;
195 return GrabSinkFrameTimeout(m_handle, image, timeout, &m_status);
196}
197
198inline uint64_t RawSink::GrabFrameNoTimeout(wpi::RawFrame& image) const {
199 m_status = 0;
200 return GrabSinkFrame(m_handle, image, &m_status);
201}
202
203} // namespace cs
204
205/** @} */
206
207#endif
208
209#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 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:112
A base class for single image reading sinks.
Definition: cscore_oo.h:1095
A base class for single image providing sources.
Definition: cscore_oo.h:750
A sink for user code to accept video frames as raw bytes.
Definition: cscore_raw.h:114
A source for user code to provide video frames as raw bytes.
Definition: cscore_raw.h:76
CS_Status m_status
Definition: cscore_oo.h:1008
CS_Sink m_handle
Definition: cscore_oo.h:1009
CS_Source m_handle
Video source handle.
Definition: cscore_oo.h:475
CS_Status m_status
Definition: cscore_oo.h:472
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_GrabRawSinkFrame(CS_Sink sink, struct WPI_RawFrame *rawImage, CS_Status *status)
RawSink()=default
uint64_t GrabFrame(wpi::RawFrame &image, double timeout=0.225) const
Wait for the next frame and get the image.
Definition: cscore_raw.h:193
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)
RawSource()=default
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:179
uint64_t GrabFrameNoTimeout(wpi::RawFrame &image) const
Wait for the next frame and get the image.
Definition: cscore_raw.h:198
CS_Sink CreateRawSinkCallback(std::string_view name, bool isCv, std::function< void(uint64_t time)> processFrame, CS_Status *status)
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: cscore_cpp.h:29
fps
Definition: velocity.h:46
constexpr const char * name(const T &)
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:75
basic_string_view< char > string_view
Definition: base.h:601
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:137