001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.cscore;
006
007import org.opencv.core.Mat;
008
009/**
010 * A source that represents a video camera. These sources require the WPILib OpenCV builds. For an
011 * alternate OpenCV, see the documentation how to build your own with RawSource.
012 */
013public class CvSource extends ImageSource {
014  /**
015   * Create an OpenCV source.
016   *
017   * @param name Source name (arbitrary unique identifier)
018   * @param mode Video mode being generated
019   */
020  public CvSource(String name, VideoMode mode) {
021    super(
022        CameraServerCvJNI.createCvSource(
023            name, mode.pixelFormat.getValue(), mode.width, mode.height, mode.fps));
024  }
025
026  /**
027   * Create an OpenCV source.
028   *
029   * @param name Source name (arbitrary unique identifier)
030   * @param pixelFormat Pixel format
031   * @param width width
032   * @param height height
033   * @param fps fps
034   */
035  public CvSource(String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
036    super(CameraServerCvJNI.createCvSource(name, pixelFormat.getValue(), width, height, fps));
037  }
038
039  /**
040   * Put an OpenCV image and notify sinks.
041   *
042   * <p>Only 8-bit single-channel or 3-channel (with BGR channel order) images are supported. If the
043   * format, depth or channel order is different, use Mat.convertTo() and/or cvtColor() to convert
044   * it first.
045   *
046   * @param image OpenCV image
047   */
048  public void putFrame(Mat image) {
049    CameraServerCvJNI.putSourceFrame(m_handle, image.nativeObj);
050  }
051}