WPILibC++ 2024.3.2
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 "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)
45
46/**
47 * Pixel formats
48 */
50 WPI_PIXFMT_UNKNOWN = 0, // unknown
51 WPI_PIXFMT_MJPEG, // Motion-JPEG (compressed image data)
52 WPI_PIXFMT_YUYV, // YUV 4:2:2, 16 bpp
53 WPI_PIXFMT_RGB565, // RGB 5-6-5, 16 bpp
54 WPI_PIXFMT_BGR, // BGR 8-8-8, 24 bpp
55 WPI_PIXFMT_GRAY, // Grayscale, 8 bpp
56 WPI_PIXFMT_Y16, // Grayscale, 16 bpp
57 WPI_PIXFMT_UYVY, // YUV 4:2:2, 16 bpp
58};
59
60// Returns nonzero if the frame data was allocated/reallocated
61int WPI_AllocateRawFrameData(WPI_RawFrame* frame, size_t requestedSize);
63void WPI_SetRawFrameData(WPI_RawFrame* frame, void* data, size_t size,
64 size_t capacity, void* cbdata,
65 void (*freeFunc)(void* cbdata, void* data,
66 size_t capacity));
67
68#ifdef __cplusplus
69} // extern "C"
70#endif
71
72#ifdef __cplusplus
73namespace wpi {
74struct RawFrame : public WPI_RawFrame {
76 data = nullptr;
77 freeFunc = nullptr;
78 freeCbData = nullptr;
79 capacity = 0;
80 size = 0;
82 width = 0;
83 height = 0;
84 }
85 RawFrame(const RawFrame&) = delete;
86 RawFrame& operator=(const RawFrame&) = delete;
87 RawFrame(RawFrame&& rhs) noexcept : WPI_RawFrame{rhs} {
88 rhs.data = nullptr;
89 rhs.freeFunc = nullptr;
90 rhs.freeCbData = nullptr;
91 rhs.capacity = 0;
92 rhs.size = 0;
93 }
94 RawFrame& operator=(RawFrame&& rhs) noexcept {
95 *static_cast<WPI_RawFrame*>(this) = rhs;
96 rhs.data = nullptr;
97 rhs.freeFunc = nullptr;
98 rhs.freeCbData = nullptr;
99 rhs.capacity = 0;
100 rhs.size = 0;
101 return *this;
102 }
103
104 void SetData(void* data, size_t size, size_t capacity, void* cbdata,
105 void (*freeFunc)(void* cbdata, void* data, size_t capacity)) {
107 }
108
109 // returns true if the frame data was allocated/reallocated
110 bool Reserve(size_t size) {
111 return WPI_AllocateRawFrameData(this, size) != 0;
112 }
113
115};
116
117#ifdef WPI_RAWFRAME_JNI
118template <std::same_as<wpi::RawFrame> T>
119void SetFrameData(JNIEnv* env, jclass rawFrameCls, jobject jframe,
120 const T& frame, bool newData) {
121 if (newData) {
122 static jmethodID setData = env->GetMethodID(rawFrameCls, "setDataJNI",
123 "(Ljava/nio/ByteBuffer;IIII)V");
124 env->CallVoidMethod(
125 jframe, setData, env->NewDirectByteBuffer(frame.data, frame.size),
126 static_cast<jint>(frame.width), static_cast<jint>(frame.height),
127 static_cast<jint>(frame.stride), static_cast<jint>(frame.pixelFormat));
128 } else {
129 static jmethodID setInfo =
130 env->GetMethodID(rawFrameCls, "setInfoJNI", "(IIII)V");
131 env->CallVoidMethod(jframe, setInfo, static_cast<jint>(frame.width),
132 static_cast<jint>(frame.height),
133 static_cast<jint>(frame.stride),
134 static_cast<jint>(frame.pixelFormat));
135 }
136}
137#endif
138
139} // namespace wpi
140#endif
141
142#endif // WPIUTIL_WPI_RAWFRAME_H_
int WPI_AllocateRawFrameData(WPI_RawFrame *frame, size_t requestedSize)
WPI_PixelFormat
Pixel formats.
Definition: RawFrame.h:49
@ WPI_PIXFMT_YUYV
Definition: RawFrame.h:52
@ WPI_PIXFMT_Y16
Definition: RawFrame.h:56
@ WPI_PIXFMT_UYVY
Definition: RawFrame.h:57
@ WPI_PIXFMT_MJPEG
Definition: RawFrame.h:51
@ WPI_PIXFMT_GRAY
Definition: RawFrame.h:55
@ WPI_PIXFMT_RGB565
Definition: RawFrame.h:53
@ WPI_PIXFMT_BGR
Definition: RawFrame.h:54
@ WPI_PIXFMT_UNKNOWN
Definition: RawFrame.h:50
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.
Definition: ntcore_cpp.h:26
Raw Frame.
Definition: RawFrame.h:32
int pixelFormat
Definition: RawFrame.h:40
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
Definition: RawFrame.h:74
RawFrame(RawFrame &&rhs) noexcept
Definition: RawFrame.h:87
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:104
RawFrame & operator=(RawFrame &&rhs) noexcept
Definition: RawFrame.h:94
bool Reserve(size_t size)
Definition: RawFrame.h:110
RawFrame(const RawFrame &)=delete
RawFrame()
Definition: RawFrame.h:75
~RawFrame()
Definition: RawFrame.h:114