Skip to content

Commit

Permalink
Update for PixelFormat/RawFrame changes (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson authored Jan 6, 2024
1 parent 56af2b7 commit 54d89c6
Showing 1 changed file with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import java.nio.ByteBuffer;

import edu.wpi.first.cscore.VideoMode;
import edu.wpi.first.cscore.ImageSink;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

import edu.wpi.first.cscore.VideoMode.PixelFormat;
import edu.wpi.first.cscore.CameraServerJNI;
import edu.wpi.first.util.PixelFormat;
import edu.wpi.first.util.RawFrame;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand All @@ -20,8 +19,7 @@ public class JavaCvSink extends ImageSink {
private ByteBuffer origByteBuffer;
private int width;
private int height;
private int pixelFormat;
private final int bgrValue = PixelFormat.kBGR.getValue();
private PixelFormat pixelFormat;

private int getCVFormat(PixelFormat pixelFormat) {
int type = 0;
Expand Down Expand Up @@ -80,22 +78,25 @@ public long grabFrame(Mat image) {
* message); the frame time is in 1 us increments.
*/
public long grabFrame(Mat image, double timeout) {
frame.setWidth(0);
frame.setHeight(0);
frame.setPixelFormat(bgrValue);
long rv = CameraServerJNI.grabSinkFrameTimeout(m_handle, frame, timeout);
frame.setInfo(0, 0, 0, PixelFormat.kBGR);
long rv = CameraServerJNI.grabRawSinkFrameTimeout(m_handle, frame, frame.getNativeObj(), timeout);
if (rv <= 0) {
return rv;
}

if (frame.getDataByteBuffer() != origByteBuffer || width != frame.getWidth() || height != frame.getHeight()
if (frame.getData() != origByteBuffer || width != frame.getWidth() || height != frame.getHeight()
|| pixelFormat != frame.getPixelFormat()) {
origByteBuffer = frame.getDataByteBuffer();
origByteBuffer = frame.getData();
height = frame.getHeight();
width = frame.getWidth();
pixelFormat = frame.getPixelFormat();
tmpMat = new Mat(frame.getHeight(), frame.getWidth(), getCVFormat(VideoMode.getPixelFormatFromInt(pixelFormat)),
origByteBuffer);
if (frame.getStride() == 0) {
tmpMat = new Mat(frame.getHeight(), frame.getWidth(), getCVFormat(pixelFormat),
origByteBuffer);
} else {
tmpMat = new Mat(frame.getHeight(), frame.getWidth(), getCVFormat(pixelFormat),
origByteBuffer, frame.getStride());
}
}
tmpMat.copyTo(image);
return rv;
Expand All @@ -110,22 +111,25 @@ public long grabFrame(Mat image, double timeout) {
* message); the frame time is in 1 us increments.
*/
public long grabFrameNoTimeout(Mat image) {
frame.setWidth(0);
frame.setHeight(0);
frame.setPixelFormat(bgrValue);
long rv = CameraServerJNI.grabSinkFrame(m_handle, frame);
frame.setInfo(0, 0, 0, PixelFormat.kBGR);
long rv = CameraServerJNI.grabRawSinkFrame(m_handle, frame, frame.getNativeObj());
if (rv <= 0) {
return rv;
}

if (frame.getDataByteBuffer() != origByteBuffer || width != frame.getWidth() || height != frame.getHeight()
if (frame.getData() != origByteBuffer || width != frame.getWidth() || height != frame.getHeight()
|| pixelFormat != frame.getPixelFormat()) {
origByteBuffer = frame.getDataByteBuffer();
origByteBuffer = frame.getData();
height = frame.getHeight();
width = frame.getWidth();
pixelFormat = frame.getPixelFormat();
tmpMat = new Mat(frame.getHeight(), frame.getWidth(), getCVFormat(VideoMode.getPixelFormatFromInt(pixelFormat)),
origByteBuffer);
if (frame.getStride() == 0) {
tmpMat = new Mat(frame.getHeight(), frame.getWidth(), getCVFormat(pixelFormat),
origByteBuffer);
} else {
tmpMat = new Mat(frame.getHeight(), frame.getWidth(), getCVFormat(pixelFormat),
origByteBuffer, frame.getStride());
}
}
tmpMat.copyTo(image);
return rv;
Expand Down

0 comments on commit 54d89c6

Please sign in to comment.