WPILibC++ 2023.4.3-108-ge5452e3
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 "cscore_c.h"
9
10#ifdef __cplusplus
11#include "cscore_oo.h"
12#endif
13
14/**
15 * Raw Frame
16 */
17typedef struct CS_RawFrame { // NOLINT
18 char* data;
21 int width;
22 int height;
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/**
31 * @defgroup cscore_raw_cfunc Raw Image Functions
32 * @{
33 */
34void CS_AllocateRawFrameData(CS_RawFrame* frame, int requestedSize);
36
38 CS_Status* status);
40 double timeout, CS_Status* status);
41
42CS_Sink CS_CreateRawSink(const char* name, CS_Status* status);
43
44CS_Sink CS_CreateRawSinkCallback(const char* name, void* data,
45 void (*processFrame)(void* data,
46 uint64_t time),
47 CS_Status* status);
48
50 CS_Status* status);
51
52CS_Source CS_CreateRawSource(const char* name, const CS_VideoMode* mode,
53 CS_Status* status);
54/** @} */
55
56#ifdef __cplusplus
57} // extern "C"
58#endif
59
60#ifdef __cplusplus
61namespace cs {
62
63struct RawFrame : public CS_RawFrame {
65 data = nullptr;
66 dataLength = 0;
68 width = 0;
69 height = 0;
70 totalData = 0;
71 }
72
74};
75
76/**
77 * @defgroup cscore_raw_func Raw Image Functions
78 * @{
79 */
80
82 CS_Status* status);
83
86 std::function<void(uint64_t time)> processFrame,
87 CS_Status* status);
88
90 CS_Status* status);
92uint64_t GrabSinkFrameTimeout(CS_Sink sink, CS_RawFrame& image, double timeout,
93 CS_Status* status);
94
95/**
96 * A source for user code to provide video frames as raw bytes.
97 *
98 * This is a complex API, most cases should use CvSource.
99 */
100class RawSource : public ImageSource {
101 public:
102 RawSource() = default;
103
104 /**
105 * Create a raw frame source.
106 *
107 * @param name Source name (arbitrary unique identifier)
108 * @param mode Video mode being generated
109 */
110 RawSource(std::string_view name, const VideoMode& mode);
111
112 /**
113 * Create a raw frame source.
114 *
115 * @param name Source name (arbitrary unique identifier)
116 * @param pixelFormat Pixel format
117 * @param width width
118 * @param height height
119 * @param fps fps
120 */
122 int width, int height, int fps);
123
124 protected:
125 /**
126 * Put a raw image and notify sinks.
127 *
128 * @param image raw frame image
129 */
130 void PutFrame(RawFrame& image);
131};
132
133/**
134 * A sink for user code to accept video frames as raw bytes.
135 *
136 * This is a complex API, most cases should use CvSource.
137 */
138class RawSink : public ImageSink {
139 public:
140 RawSink() = default;
141
142 /**
143 * Create a sink for accepting raw images.
144 *
145 * <p>GrabFrame() must be called on the created sink to get each new
146 * image.
147 *
148 * @param name Source name (arbitrary unique identifier)
149 */
150 explicit RawSink(std::string_view name);
151
152 /**
153 * Create a sink for accepting raws images in a separate thread.
154 *
155 * <p>A thread will be created that calls WaitForFrame() and calls the
156 * processFrame() callback each time a new frame arrives.
157 *
158 * @param name Source name (arbitrary unique identifier)
159 * @param processFrame Frame processing function; will be called with a
160 * time=0 if an error occurred. processFrame should call GetImage()
161 * or GetError() as needed, but should not call (except in very
162 * unusual circumstances) WaitForImage().
163 */
165 std::function<void(uint64_t time)> processFrame);
166
167 protected:
168 /**
169 * Wait for the next frame and get the image.
170 * Times out (returning 0) after timeout seconds.
171 * The provided image will have three 8-bit channels stored in BGR order.
172 *
173 * @return Frame time, or 0 on error (call GetError() to obtain the error
174 * message); the frame time is in the same time base as wpi::Now(),
175 * and is in 1 us increments.
176 */
177 [[nodiscard]]
178 uint64_t GrabFrame(RawFrame& image, double timeout = 0.225) const;
179
180 /**
181 * Wait for the next frame and get the image. May block forever.
182 * The provided image will have three 8-bit channels stored in BGR order.
183 *
184 * @return Frame time, or 0 on error (call GetError() to obtain the error
185 * message); the frame time is in the same time base as wpi::Now(),
186 * and is in 1 us increments.
187 */
188 [[nodiscard]]
190};
191
193 m_handle = CreateRawSource(name, mode, &m_status);
194}
195
198 int height, int fps) {
199 m_handle =
200 CreateRawSource(name, VideoMode{format, width, height, fps}, &m_status);
201}
202
203inline void RawSource::PutFrame(RawFrame& image) {
204 m_status = 0;
206}
207
210}
211
213 std::function<void(uint64_t time)> processFrame) {
214 m_handle = CreateRawSinkCallback(name, processFrame, &m_status);
215}
216
217inline uint64_t RawSink::GrabFrame(RawFrame& image, double timeout) const {
218 m_status = 0;
219 return GrabSinkFrameTimeout(m_handle, image, timeout, &m_status);
220}
221
223 m_status = 0;
224 return GrabSinkFrame(m_handle, image, &m_status);
225}
226
227} // namespace cs
228
229/** @} */
230
231#endif
232
233#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:111
A base class for single image reading sinks.
Definition: cscore_oo.h:940
A base class for single image providing sources.
Definition: cscore_oo.h:610
A sink for user code to accept video frames as raw bytes.
Definition: cscore_raw.h:138
A source for user code to provide video frames as raw bytes.
Definition: cscore_raw.h:100
CS_Status m_status
Definition: cscore_oo.h:853
CS_Sink m_handle
Definition: cscore_oo.h:854
CS_Source m_handle
Definition: cscore_oo.h:352
CS_Status m_status
Definition: cscore_oo.h:351
basic_string_view< char > string_view
Definition: core.h:520
struct CS_RawFrame CS_RawFrame
Raw Frame.
@ CS_PIXFMT_UNKNOWN
Definition: cscore_c.h:91
uint64_t CS_GrabRawSinkFrame(CS_Sink sink, struct CS_RawFrame *rawImage, CS_Status *status)
CS_Sink CS_CreateRawSinkCallback(const char *name, void *data, void(*processFrame)(void *data, uint64_t time), CS_Status *status)
uint64_t CS_GrabRawSinkFrameTimeout(CS_Sink sink, struct CS_RawFrame *rawImage, double timeout, CS_Status *status)
void CS_PutRawSourceFrame(CS_Source source, const struct CS_RawFrame *image, CS_Status *status)
CS_Sink CS_CreateRawSink(const char *name, CS_Status *status)
void CS_AllocateRawFrameData(CS_RawFrame *frame, int requestedSize)
CS_Source CS_CreateRawSource(const char *name, const CS_VideoMode *mode, CS_Status *status)
void CS_FreeRawFrameData(CS_RawFrame *frame)
void PutFrame(RawFrame &image)
Put a raw image and notify sinks.
Definition: cscore_raw.h:203
~RawFrame()
Definition: cscore_raw.h:73
RawSink()=default
CS_Source CreateRawSource(std::string_view name, const VideoMode &mode, CS_Status *status)
uint64_t GrabFrame(RawFrame &image, double timeout=0.225) const
Wait for the next frame and get the image.
Definition: cscore_raw.h:217
RawFrame()
Definition: cscore_raw.h:64
RawSource()=default
CS_Sink CreateRawSink(std::string_view name, CS_Status *status)
CS_Sink CreateRawSinkCallback(std::string_view name, std::function< void(uint64_t time)> processFrame, CS_Status *status)
uint64_t GrabFrameNoTimeout(RawFrame &image) const
Wait for the next frame and get the image.
Definition: cscore_raw.h:222
CS_Handle CS_Source
Definition: cscore_c.h:51
int CS_Status
Definition: cscore_c.h:44
CS_Handle CS_Sink
Definition: cscore_c.h:50
::uint64_t uint64_t
Definition: Meta.h:58
CameraServer (cscore) namespace.
Definition: cscore_oo.inc:15
void PutSourceFrame(CS_Source source, cv::Mat &image, CS_Status *status)
uint64_t GrabSinkFrame(CS_Sink sink, cv::Mat &image, CS_Status *status)
uint64_t GrabSinkFrameTimeout(CS_Sink sink, cv::Mat &image, double timeout, CS_Status *status)
fps
Definition: velocity.h:46
Raw Frame.
Definition: cscore_raw.h:17
int totalData
Definition: cscore_raw.h:23
int dataLength
Definition: cscore_raw.h:19
int width
Definition: cscore_raw.h:21
int pixelFormat
Definition: cscore_raw.h:20
char * data
Definition: cscore_raw.h:18
int height
Definition: cscore_raw.h:22
Video mode.
Definition: cscore_c.h:104
Definition: cscore_raw.h:63
Video mode.
Definition: cscore_cpp.h:64
PixelFormat
Definition: cscore_cpp.h:65
Definition: format.h:1552
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:87