Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpiutil] Add a RawFrame JNI overload for byte[] #7179

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions wpiutil/src/main/java/edu/wpi/first/util/RawFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ public void setData(ByteBuffer data, int width, int height, int stride, PixelFor
m_nativeObj, data, data.limit(), width, height, stride, pixelFormat.getValue());
}

/**
* Set frame data.
*
* @param data A Java byte[] pointing to the frame data.
* @param width The width of the frame, in pixels
* @param height The height of the frame, in pixels
* @param stride The number of bytes in each row of image data
* @param pixelFormat The PixelFormat of the frame
*/
public void setData(byte[] data, int width, int height, int stride, PixelFormat pixelFormat) {
m_data = ByteBuffer.wrap(data);
spacey-sooty marked this conversation as resolved.
Show resolved Hide resolved
m_width = width;
m_height = height;
m_stride = stride;
m_pixelFormat = pixelFormat;
WPIUtilJNI.setRawFrameDataArray(
m_nativeObj, data, data.length, width, height, stride, pixelFormat.getValue());
}

/**
* Call to set frame information.
*
Expand Down
3 changes: 3 additions & 0 deletions wpiutil/src/main/java/edu/wpi/first/util/WPIUtilJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ public static synchronized void forceLoad() throws IOException {
static native void setRawFrameData(
long frame, ByteBuffer data, int size, int width, int height, int stride, int pixelFormat);

static native void setRawFrameDataArray(
long frame, byte[] data, int size, int width, int height, int stride, int pixelFormat);

static native void setRawFrameInfo(
long frame, int size, int width, int height, int stride, int pixelFormat);

Expand Down
29 changes: 29 additions & 0 deletions wpiutil/src/main/native/cpp/jni/WPIUtilJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,35 @@ Java_edu_wpi_first_util_WPIUtilJNI_setRawFrameData
f->pixelFormat = pixelFormat;
}

/*
* Class: edu_wpi_first_util_WPIUtilJNI
* Method: setRawFrameDataArray
* Signature: (J[BIIIII)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_util_WPIUtilJNI_setRawFrameDataArray
(JNIEnv* env, jclass, jlong frame, jbyteArray data, jint size, jint width,
jint height, jint stride, jint pixelFormat)
{
auto* f = reinterpret_cast<wpi::RawFrame*>(frame);
if (!f) {
wpi::ThrowNullPointerException(env, "frame is null");
return;
}
auto buf = reinterpret_cast<jbyte*>(env->GetByteArrayElements(data, NULL));
if (!buf) {
wpi::ThrowNullPointerException(env, "data is null");
return;
}
// there's no way to free a passed-in direct byte buffer
f->SetData(buf, size, env->GetDirectBufferCapacity(data), nullptr,
[](void*, void*, size_t) {});
f->width = width;
f->height = height;
f->stride = stride;
f->pixelFormat = pixelFormat;
}

/*
* Class: edu_wpi_first_util_WPIUtilJNI
* Method: setRawFrameInfo
Expand Down
Loading