Rename our allwpilib (which is now 2020) to not have 2019 in the name
Change-Id: I3c07f85ed32ab8b97db765a9b43f2a6ce7da964a
diff --git a/cscore/java-examples/RawCVMatSink.java b/cscore/java-examples/RawCVMatSink.java
new file mode 100644
index 0000000..d9557f7
--- /dev/null
+++ b/cscore/java-examples/RawCVMatSink.java
@@ -0,0 +1,96 @@
+/*----------------------------------------------------------------------------*/
+/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
+/* Open Source Software - may be modified and shared by FRC teams. The code */
+/* must be accompanied by the FIRST BSD license file in the root directory of */
+/* the project. */
+/*----------------------------------------------------------------------------*/
+
+package edu.wpi.cscore;
+
+import java.nio.ByteBuffer;
+
+import org.opencv.core.CvType;
+import org.opencv.core.Mat;
+
+import edu.wpi.cscore.VideoMode.PixelFormat;
+import edu.wpi.cscore.raw.RawFrame;
+
+public class RawCVMatSink extends ImageSink {
+ RawFrame frame = new RawFrame();
+ Mat tmpMat;
+ ByteBuffer origByteBuffer;
+ int width;
+ int height;
+ int pixelFormat;
+ int bgrValue = PixelFormat.kBGR.getValue();
+
+ private int getCVFormat(PixelFormat pixelFormat) {
+ int type = 0;
+ switch (pixelFormat) {
+ case kYUYV:
+ case kRGB565:
+ type = CvType.CV_8UC2;
+ break;
+ case kBGR:
+ type = CvType.CV_8UC3;
+ break;
+ case kGray:
+ case kMJPEG:
+ default:
+ type = CvType.CV_8UC1;
+ break;
+ }
+ return type;
+ }
+
+ /**
+ * Create a sink for accepting OpenCV images.
+ * WaitForFrame() must be called on the created sink to get each new
+ * image.
+ *
+ * @param name Source name (arbitrary unique identifier)
+ */
+ public RawCVMatSink(String name) {
+ super(CameraServerJNI.createRawSink(name));
+ }
+
+ /**
+ * Wait for the next frame and get the image.
+ * Times out (returning 0) after 0.225 seconds.
+ * The provided image will have three 3-bit channels stored in BGR order.
+ *
+ * @return Frame time, or 0 on error (call GetError() to obtain the error
+ * message)
+ */
+ public long grabFrame(Mat image) {
+ return grabFrame(image, 0.225);
+ }
+
+ /**
+ * Wait for the next frame and get the image.
+ * Times out (returning 0) after timeout seconds.
+ * The provided image will have three 3-bit channels stored in BGR order.
+ *
+ * @return Frame time, or 0 on error (call GetError() to obtain the error
+ * 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);
+ if (rv <= 0) {
+ return rv;
+ }
+
+ if (frame.getDataByteBuffer() != origByteBuffer || width != frame.getWidth() || height != frame.getHeight() || pixelFormat != frame.getPixelFormat()) {
+ origByteBuffer = frame.getDataByteBuffer();
+ height = frame.getHeight();
+ width = frame.getWidth();
+ pixelFormat = frame.getPixelFormat();
+ tmpMat = new Mat(frame.getHeight(), frame.getWidth(), getCVFormat(VideoMode.getPixelFormatFromInt(pixelFormat)), origByteBuffer);
+ }
+ tmpMat.copyTo(image);
+ return rv;
+ }
+}
diff --git a/cscore/java-examples/RawCVMatSource.java b/cscore/java-examples/RawCVMatSource.java
new file mode 100644
index 0000000..65bd7d2
--- /dev/null
+++ b/cscore/java-examples/RawCVMatSource.java
@@ -0,0 +1,59 @@
+/*----------------------------------------------------------------------------*/
+/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
+/* Open Source Software - may be modified and shared by FRC teams. The code */
+/* must be accompanied by the FIRST BSD license file in the root directory of */
+/* the project. */
+/*----------------------------------------------------------------------------*/
+
+package edu.wpi.cscore;
+
+import org.opencv.core.Mat;
+
+import edu.wpi.cscore.VideoMode.PixelFormat;
+
+public class RawCVMatSource extends ImageSource {
+ /**
+ * Create an OpenCV source.
+ *
+ * @param name Source name (arbitrary unique identifier)
+ * @param mode Video mode being generated
+ */
+ public RawCVMatSource(String name, VideoMode mode) {
+ super(CameraServerJNI.createRawSource(name,
+ mode.pixelFormat.getValue(),
+ mode.width,
+ mode.height,
+ mode.fps));
+ }
+
+ /**
+ * Create an OpenCV source.
+ *
+ * @param name Source name (arbitrary unique identifier)
+ * @param pixelFormat Pixel format
+ * @param width width
+ * @param height height
+ * @param fps fps
+ */
+ public RawCVMatSource(String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
+ super(CameraServerJNI.createRawSource(name, pixelFormat.getValue(), width, height, fps));
+ }
+
+ /**
+ * Put an OpenCV image and notify sinks.
+ *
+ * <p>Only 8-bit single-channel or 3-channel (with BGR channel order) images
+ * are supported. If the format, depth or channel order is different, use
+ * Mat.convertTo() and/or cvtColor() to convert it first.
+ *
+ * @param image OpenCV image
+ */
+ public void putFrame(Mat image) {
+ int channels = image.channels();
+ if (channels != 1 && channels != 3) {
+ throw new VideoException("Unsupported Image Type");
+ }
+ int imgType = channels == 1 ? PixelFormat.kGray.getValue() : PixelFormat.kBGR.getValue();
+ CameraServerJNI.putRawSourceFrame(m_handle, image.dataAddr(), image.width(), image.height(), imgType, (int)image.total() * channels);
+ }
+}