2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-05-30 19:12:05 -07:00
|
|
|
|
2023-11-23 13:55:10 -05:00
|
|
|
package edu.wpi.first.util;
|
2019-05-30 19:12:05 -07:00
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
import java.nio.ByteBuffer;
|
2019-05-30 19:12:05 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class for storing raw frame data between image read call.
|
|
|
|
|
*
|
|
|
|
|
* <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 int m_width;
|
|
|
|
|
private int m_height;
|
|
|
|
|
private int m_pixelFormat;
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Construct a new RawFrame. */
|
2019-05-30 19:12:05 -07:00
|
|
|
public RawFrame() {
|
2023-11-23 13:55:10 -05:00
|
|
|
m_framePtr = WPIUtilJNI.allocateRawFrame();
|
2019-05-30 19:12:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-29 22:45:16 -08:00
|
|
|
* Close the RawFrame, releasing native resources. Any images currently using the data will be
|
|
|
|
|
* invalidated.
|
2019-05-30 19:12:05 -07:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void close() {
|
2023-11-23 13:55:10 -05:00
|
|
|
WPIUtilJNI.freeRawFrame(m_framePtr);
|
2019-05-30 19:12:05 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2020-12-29 22:45:16 -08:00
|
|
|
public void setData(
|
|
|
|
|
ByteBuffer dataByteBuffer,
|
|
|
|
|
long dataPtr,
|
|
|
|
|
int totalData,
|
|
|
|
|
int width,
|
|
|
|
|
int height,
|
|
|
|
|
int pixelFormat) {
|
2019-05-30 19:12:05 -07:00
|
|
|
m_dataByteBuffer = dataByteBuffer;
|
|
|
|
|
m_dataPtr = dataPtr;
|
|
|
|
|
m_totalData = totalData;
|
|
|
|
|
m_width = width;
|
|
|
|
|
m_height = height;
|
|
|
|
|
m_pixelFormat = pixelFormat;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Get the pointer to native representation of this frame.
|
|
|
|
|
*
|
|
|
|
|
* @return The pointer to native representation of this frame.
|
|
|
|
|
*/
|
2019-05-30 19:12:05 -07:00
|
|
|
public long getFramePtr() {
|
|
|
|
|
return m_framePtr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-29 22:45:16 -08:00
|
|
|
* Get a ByteBuffer pointing to the frame data. This ByteBuffer 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.
|
2021-06-10 20:46:47 -07:00
|
|
|
*
|
|
|
|
|
* @return A ByteBuffer pointing to the frame data.
|
2019-05-30 19:12:05 -07:00
|
|
|
*/
|
|
|
|
|
public ByteBuffer getDataByteBuffer() {
|
|
|
|
|
return m_dataByteBuffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-29 22:45:16 -08:00
|
|
|
* Get a long (is a char* 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.
|
2021-06-10 20:46:47 -07:00
|
|
|
*
|
|
|
|
|
* @return A long pointing to the frame data.
|
2019-05-30 19:12:05 -07:00
|
|
|
*/
|
|
|
|
|
public long getDataPtr() {
|
|
|
|
|
return m_dataPtr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Get the total length of the data stored in the frame.
|
|
|
|
|
*
|
|
|
|
|
* @return The total length of the data stored in the frame.
|
|
|
|
|
*/
|
2019-05-30 19:12:05 -07:00
|
|
|
public int getTotalData() {
|
|
|
|
|
return m_totalData;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Get the width of the frame.
|
|
|
|
|
*
|
|
|
|
|
* @return The width of the frame.
|
|
|
|
|
*/
|
2019-05-30 19:12:05 -07:00
|
|
|
public int getWidth() {
|
|
|
|
|
return m_width;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Set the width of the frame.
|
|
|
|
|
*
|
|
|
|
|
* @param width The width of the frame.
|
|
|
|
|
*/
|
2019-05-30 19:12:05 -07:00
|
|
|
public void setWidth(int width) {
|
|
|
|
|
this.m_width = width;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Get the height of the frame.
|
|
|
|
|
*
|
|
|
|
|
* @return The height of the frame.
|
|
|
|
|
*/
|
2019-05-30 19:12:05 -07:00
|
|
|
public int getHeight() {
|
|
|
|
|
return m_height;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Set the height of the frame.
|
|
|
|
|
*
|
|
|
|
|
* @param height The height of the frame.
|
|
|
|
|
*/
|
2019-05-30 19:12:05 -07:00
|
|
|
public void setHeight(int height) {
|
|
|
|
|
this.m_height = height;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Get the PixelFormat of the frame.
|
|
|
|
|
*
|
|
|
|
|
* @return The PixelFormat of the frame.
|
|
|
|
|
*/
|
2019-05-30 19:12:05 -07:00
|
|
|
public int getPixelFormat() {
|
|
|
|
|
return m_pixelFormat;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Set the PixelFormat of the frame.
|
|
|
|
|
*
|
|
|
|
|
* @param pixelFormat The PixelFormat of the frame.
|
|
|
|
|
*/
|
2019-05-30 19:12:05 -07:00
|
|
|
public void setPixelFormat(int pixelFormat) {
|
|
|
|
|
this.m_pixelFormat = pixelFormat;
|
|
|
|
|
}
|
|
|
|
|
}
|