WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
RawFrame.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 WPIUTIL_WPI_RAWFRAME_H_
6#define WPIUTIL_WPI_RAWFRAME_H_
7
8#include <stdint.h>
9
10#ifdef __cplusplus
11#include <concepts>
12#include <cstddef>
13#else
14
15#include <stddef.h> // NOLINT
16
17#endif
18
19#ifdef WPI_RAWFRAME_JNI
20#include "wpi/jni_util.h"
21#endif
22
23// NOLINT
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/**
30 * Raw Frame
31 */
32typedef struct WPI_RawFrame { // NOLINT
33 // image data
34 uint8_t* data;
35 // function to free image data (may be NULL)
36 void (*freeFunc)(void* cbdata, void* data, size_t capacity);
37 void* freeCbData; // data passed to freeFunc
38 size_t capacity; // data buffer capacity, in bytes
39 size_t size; // actual size of data, in bytes
40 int pixelFormat; // WPI_PixelFormat
41 int width; // width of image, in pixels
42 int height; // height of image, in pixels
43 int stride; // size of each row of data, in bytes (may be 0)
44 uint64_t timestamp; // image capture timestamp
45 int timestampSrc; // WPI_TimestampSource
47
48/**
49 * Pixel formats
50 */
52 WPI_PIXFMT_UNKNOWN = 0, // unknown
53 WPI_PIXFMT_MJPEG, // Motion-JPEG (compressed image data)
54 WPI_PIXFMT_YUYV, // YUV 4:2:2, 16 bpp
55 WPI_PIXFMT_RGB565, // RGB 5-6-5, 16 bpp
56 WPI_PIXFMT_BGR, // BGR 8-8-8, 24 bpp
57 WPI_PIXFMT_GRAY, // Grayscale, 8 bpp
58 WPI_PIXFMT_Y16, // Grayscale, 16 bpp
59 WPI_PIXFMT_UYVY, // YUV 4:2:2, 16 bpp
60 WPI_PIXFMT_BGRA, // BGRA 8-8-8-8-, 32 bpp
61};
62
63/**
64 * Timestamp metadata. Timebase is the same as wpi::Now
65 */
67 WPI_TIMESRC_UNKNOWN = 0, // unknown
68 WPI_TIMESRC_FRAME_DEQUEUE, // wpi::Now when the new frame was dequeued by
69 // CSCore. Does not account for camera exposure
70 // time or V4L latency.
71 WPI_TIMESRC_V4L_EOF, // End of Frame. Same as V4L2_BUF_FLAG_TSTAMP_SRC_EOF,
72 // translated into wpi::Now's timebase.
73 WPI_TIMESRC_V4L_SOE, // Start of Exposure. Same as
74 // V4L2_BUF_FLAG_TSTAMP_SRC_SOE, translated into
75 // wpi::Now's timebase.
76};
77
78// Returns nonzero if the frame data was allocated/reallocated
79int WPI_AllocateRawFrameData(WPI_RawFrame* frame, size_t requestedSize);
81void WPI_SetRawFrameData(WPI_RawFrame* frame, void* data, size_t size,
82 size_t capacity, void* cbdata,
83 void (*freeFunc)(void* cbdata, void* data,
84 size_t capacity));
85
86#ifdef __cplusplus
87} // extern "C"
88#endif
89
90#ifdef __cplusplus
91namespace wpi {
92struct RawFrame : public WPI_RawFrame {
94 data = nullptr;
95 freeFunc = nullptr;
96 freeCbData = nullptr;
97 capacity = 0;
98 size = 0;
100 width = 0;
101 height = 0;
102 timestamp = 0;
104 }
105 RawFrame(const RawFrame&) = delete;
106 RawFrame& operator=(const RawFrame&) = delete;
107 RawFrame(RawFrame&& rhs) noexcept : WPI_RawFrame{rhs} {
108 rhs.data = nullptr;
109 rhs.freeFunc = nullptr;
110 rhs.freeCbData = nullptr;
111 rhs.capacity = 0;
112 rhs.size = 0;
113 }
114 RawFrame& operator=(RawFrame&& rhs) noexcept {
115 *static_cast<WPI_RawFrame*>(this) = rhs;
116 rhs.data = nullptr;
117 rhs.freeFunc = nullptr;
118 rhs.freeCbData = nullptr;
119 rhs.capacity = 0;
120 rhs.size = 0;
121 return *this;
122 }
123
124 void SetData(void* data, size_t size, size_t capacity, void* cbdata,
125 void (*freeFunc)(void* cbdata, void* data, size_t capacity)) {
126 WPI_SetRawFrameData(this, data, size, capacity, cbdata, freeFunc);
127 }
128
129 // returns true if the frame data was allocated/reallocated
130 bool Reserve(size_t size) {
131 return WPI_AllocateRawFrameData(this, size) != 0;
132 }
133
135};
136
137#ifdef WPI_RAWFRAME_JNI
138template <std::same_as<wpi::RawFrame> T>
139void SetFrameData(JNIEnv* env, jclass rawFrameCls, jobject jframe,
140 const T& frame, bool newData) {
141 if (newData) {
142 static jmethodID setData = env->GetMethodID(
143 rawFrameCls, "setDataJNI", "(Ljava/nio/ByteBuffer;IIIIJI)V");
144 env->CallVoidMethod(
145 jframe, setData, env->NewDirectByteBuffer(frame.data, frame.size),
146 static_cast<jint>(frame.width), static_cast<jint>(frame.height),
147 static_cast<jint>(frame.stride), static_cast<jint>(frame.pixelFormat),
148 static_cast<jlong>(frame.timestamp),
149 static_cast<jint>(frame.timestampSrc));
150 } else {
151 static jmethodID setInfo =
152 env->GetMethodID(rawFrameCls, "setInfoJNI", "(IIIIJI)V");
153 env->CallVoidMethod(jframe, setInfo, static_cast<jint>(frame.width),
154 static_cast<jint>(frame.height),
155 static_cast<jint>(frame.stride),
156 static_cast<jint>(frame.pixelFormat),
157 static_cast<jlong>(frame.timestamp),
158 static_cast<jint>(frame.timestampSrc));
159 }
160}
161#endif
162
163} // namespace wpi
164#endif
165
166#endif // WPIUTIL_WPI_RAWFRAME_H_
int WPI_AllocateRawFrameData(WPI_RawFrame *frame, size_t requestedSize)
WPI_PixelFormat
Pixel formats.
Definition RawFrame.h:51
@ WPI_PIXFMT_YUYV
Definition RawFrame.h:54
@ WPI_PIXFMT_Y16
Definition RawFrame.h:58
@ WPI_PIXFMT_UYVY
Definition RawFrame.h:59
@ WPI_PIXFMT_BGRA
Definition RawFrame.h:60
@ WPI_PIXFMT_MJPEG
Definition RawFrame.h:53
@ WPI_PIXFMT_GRAY
Definition RawFrame.h:57
@ WPI_PIXFMT_RGB565
Definition RawFrame.h:55
@ WPI_PIXFMT_BGR
Definition RawFrame.h:56
@ WPI_PIXFMT_UNKNOWN
Definition RawFrame.h:52
WPI_TimestampSource
Timestamp metadata.
Definition RawFrame.h:66
@ WPI_TIMESRC_UNKNOWN
Definition RawFrame.h:67
@ WPI_TIMESRC_V4L_SOE
Definition RawFrame.h:73
@ WPI_TIMESRC_V4L_EOF
Definition RawFrame.h:71
@ WPI_TIMESRC_FRAME_DEQUEUE
Definition RawFrame.h:68
void WPI_SetRawFrameData(WPI_RawFrame *frame, void *data, size_t size, size_t capacity, void *cbdata, void(*freeFunc)(void *cbdata, void *data, size_t capacity))
void WPI_FreeRawFrameData(WPI_RawFrame *frame)
struct WPI_RawFrame WPI_RawFrame
Raw Frame.
Foonathan namespace.
Definition ntcore_cpp.h:26
Raw Frame.
Definition RawFrame.h:32
int pixelFormat
Definition RawFrame.h:40
uint64_t timestamp
Definition RawFrame.h:44
void * freeCbData
Definition RawFrame.h:37
void(* freeFunc)(void *cbdata, void *data, size_t capacity)
Definition RawFrame.h:36
size_t capacity
Definition RawFrame.h:38
uint8_t * data
Definition RawFrame.h:34
size_t size
Definition RawFrame.h:39
int height
Definition RawFrame.h:42
int stride
Definition RawFrame.h:43
int width
Definition RawFrame.h:41
int timestampSrc
Definition RawFrame.h:45
Definition RawFrame.h:92
RawFrame(RawFrame &&rhs) noexcept
Definition RawFrame.h:107
RawFrame & operator=(const RawFrame &)=delete
void SetData(void *data, size_t size, size_t capacity, void *cbdata, void(*freeFunc)(void *cbdata, void *data, size_t capacity))
Definition RawFrame.h:124
RawFrame & operator=(RawFrame &&rhs) noexcept
Definition RawFrame.h:114
bool Reserve(size_t size)
Definition RawFrame.h:130
RawFrame(const RawFrame &)=delete
RawFrame()
Definition RawFrame.h:93
~RawFrame()
Definition RawFrame.h:134