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