[wpiutil,cscore,apriltag] Fix RawFrame (#6098)

This commit is contained in:
Peter Johnson
2023-12-26 20:05:02 -08:00
committed by GitHub
parent 8aeee03626
commit 5659038443
18 changed files with 537 additions and 317 deletions

View File

@@ -12,17 +12,16 @@ import java.nio.ByteBuffer;
* <p>Data is reused for each frame read, rather then reallocating every frame.
*/
public class RawFrame implements AutoCloseable {
private final long m_framePtr;
private ByteBuffer m_dataByteBuffer;
private long m_dataPtr;
private int m_totalData;
private long m_nativeObj;
private ByteBuffer m_data;
private int m_width;
private int m_height;
private int m_pixelFormat;
private int m_stride;
private PixelFormat m_pixelFormat;
/** Construct a new RawFrame. */
/** Construct a new empty RawFrame. */
public RawFrame() {
m_framePtr = WPIUtilJNI.allocateRawFrame();
m_nativeObj = WPIUtilJNI.allocateRawFrame();
}
/**
@@ -31,32 +30,79 @@ public class RawFrame implements AutoCloseable {
*/
@Override
public void close() {
WPIUtilJNI.freeRawFrame(m_framePtr);
WPIUtilJNI.freeRawFrame(m_nativeObj);
m_nativeObj = 0;
}
/**
* Called from JNI to set data in class.
*
* @param dataByteBuffer A ByteBuffer pointing to the frame data.
* @param dataPtr A long (a char* in native code) pointing to the frame data.
* @param totalData The total length of the data stored in the frame.
* @param width The width of the frame.
* @param height The height of the frame.
* @param pixelFormat The PixelFormat of the frame.
* @param data A native ByteBuffer 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(
ByteBuffer dataByteBuffer,
long dataPtr,
int totalData,
int width,
int height,
int pixelFormat) {
m_dataByteBuffer = dataByteBuffer;
m_dataPtr = dataPtr;
m_totalData = totalData;
void setDataJNI(ByteBuffer data, int width, int height, int stride, int pixelFormat) {
m_data = data;
m_width = width;
m_height = height;
m_stride = stride;
m_pixelFormat = PixelFormat.getFromInt(pixelFormat);
}
/**
* Called from JNI to set info in class.
*
* @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
*/
void setInfoJNI(int width, int height, int stride, int pixelFormat) {
m_width = width;
m_height = height;
m_stride = stride;
m_pixelFormat = PixelFormat.getFromInt(pixelFormat);
}
/**
* Set frame data.
*
* @param data A native ByteBuffer 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(ByteBuffer data, int width, int height, int stride, PixelFormat pixelFormat) {
if (!data.isDirect()) {
throw new UnsupportedOperationException("ByteBuffer must be direct");
}
m_data = data;
m_width = width;
m_height = height;
m_stride = stride;
m_pixelFormat = pixelFormat;
WPIUtilJNI.setRawFrameData(
m_nativeObj, data, data.limit(), width, height, stride, pixelFormat.getValue());
}
/**
* Call to set frame information.
*
* @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 setInfo(int width, int height, int stride, PixelFormat pixelFormat) {
m_width = width;
m_height = height;
m_stride = stride;
m_pixelFormat = pixelFormat;
WPIUtilJNI.setRawFrameInfo(
m_nativeObj, m_data.limit(), width, height, stride, pixelFormat.getValue());
}
/**
@@ -64,8 +110,8 @@ public class RawFrame implements AutoCloseable {
*
* @return The pointer to native representation of this frame.
*/
public long getFramePtr() {
return m_framePtr;
public long getNativeObj() {
return m_nativeObj;
}
/**
@@ -75,64 +121,55 @@ public class RawFrame implements AutoCloseable {
*
* @return A ByteBuffer pointing to the frame data.
*/
public ByteBuffer getDataByteBuffer() {
return m_dataByteBuffer;
public ByteBuffer getData() {
return m_data;
}
/**
* Get a long (is a char* in native code) pointing to the frame data. This pointer is backed by
* Get a long (is a uint8_t* in native code) pointing to the frame data. This pointer is backed by
* the frame directly. Its lifetime is controlled by the frame. If a new frame gets read, it will
* overwrite the current one.
*
* @return A long pointing to the frame data.
*/
public long getDataPtr() {
return m_dataPtr;
return WPIUtilJNI.getRawFrameDataPtr(m_nativeObj);
}
/**
* Get the total length of the data stored in the frame.
* Get the total size of the data stored in the frame, in bytes.
*
* @return The total length of the data stored in the frame.
* @return The total size of the data stored in the frame.
*/
public int getTotalData() {
return m_totalData;
public int getSize() {
return m_data.limit();
}
/**
* Get the width of the frame.
* Get the width of the image.
*
* @return The width of the frame.
* @return The width of the image, in pixels.
*/
public int getWidth() {
return m_width;
}
/**
* Set the width of the frame.
* Get the height of the image.
*
* @param width The width of the frame.
*/
public void setWidth(int width) {
this.m_width = width;
}
/**
* Get the height of the frame.
*
* @return The height of the frame.
* @return The height of the image, in pixels.
*/
public int getHeight() {
return m_height;
}
/**
* Set the height of the frame.
* Get the number of bytes in each row of image data.
*
* @param height The height of the frame.
* @return The image data stride, in bytes.
*/
public void setHeight(int height) {
this.m_height = height;
public int getStride() {
return m_stride;
}
/**
@@ -140,16 +177,7 @@ public class RawFrame implements AutoCloseable {
*
* @return The PixelFormat of the frame.
*/
public int getPixelFormat() {
public PixelFormat getPixelFormat() {
return m_pixelFormat;
}
/**
* Set the PixelFormat of the frame.
*
* @param pixelFormat The PixelFormat of the frame.
*/
public void setPixelFormat(int pixelFormat) {
this.m_pixelFormat = pixelFormat;
}
}