blob: e000ae3dd163932d11f5eb456056a4b75c190491 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5package edu.wpi.cscore;
6
Brian Silverman8fce7482020-01-05 13:18:21 -08007import edu.wpi.cscore.VideoMode.PixelFormat;
Austin Schuh812d0d12021-11-04 20:16:48 -07008import org.opencv.core.Mat;
Brian Silverman8fce7482020-01-05 13:18:21 -08009
10public class RawCVMatSource extends ImageSource {
11 /**
12 * Create an OpenCV source.
13 *
14 * @param name Source name (arbitrary unique identifier)
15 * @param mode Video mode being generated
16 */
17 public RawCVMatSource(String name, VideoMode mode) {
Austin Schuh812d0d12021-11-04 20:16:48 -070018 super(
19 CameraServerJNI.createRawSource(
20 name, mode.pixelFormat.getValue(), mode.width, mode.height, mode.fps));
Brian Silverman8fce7482020-01-05 13:18:21 -080021 }
22
23 /**
24 * Create an OpenCV source.
25 *
26 * @param name Source name (arbitrary unique identifier)
27 * @param pixelFormat Pixel format
28 * @param width width
29 * @param height height
30 * @param fps fps
31 */
Austin Schuh812d0d12021-11-04 20:16:48 -070032 public RawCVMatSource(
33 String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
Brian Silverman8fce7482020-01-05 13:18:21 -080034 super(CameraServerJNI.createRawSource(name, pixelFormat.getValue(), width, height, fps));
35 }
36
37 /**
38 * Put an OpenCV image and notify sinks.
39 *
Austin Schuh812d0d12021-11-04 20:16:48 -070040 * <p>Only 8-bit single-channel or 3-channel (with BGR channel order) images are supported. If the
41 * format, depth or channel order is different, use Mat.convertTo() and/or cvtColor() to convert
42 * it first.
Brian Silverman8fce7482020-01-05 13:18:21 -080043 *
44 * @param image OpenCV image
45 */
46 public void putFrame(Mat image) {
47 int channels = image.channels();
48 if (channels != 1 && channels != 3) {
49 throw new VideoException("Unsupported Image Type");
50 }
51 int imgType = channels == 1 ? PixelFormat.kGray.getValue() : PixelFormat.kBGR.getValue();
Austin Schuh812d0d12021-11-04 20:16:48 -070052 CameraServerJNI.putRawSourceFrame(
53 m_handle,
54 image.dataAddr(),
55 image.width(),
56 image.height(),
57 imgType,
58 (int) image.total() * channels);
Brian Silverman8fce7482020-01-05 13:18:21 -080059 }
60}