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.raw;
006
007import edu.wpi.first.cscore.CameraServerJNI;
008import edu.wpi.first.cscore.ImageSource;
009import edu.wpi.first.cscore.VideoMode;
010import edu.wpi.first.util.PixelFormat;
011import edu.wpi.first.util.RawFrame;
012import java.nio.ByteBuffer;
013
014/**
015 * A source for user code to provide video frames as raw bytes.
016 *
017 * <p>This is a complex API, most cases should use CvSource.
018 */
019public class RawSource extends ImageSource {
020  /**
021   * Create a raw frame source.
022   *
023   * @param name Source name (arbitrary unique identifier)
024   * @param mode Video mode being generated
025   */
026  public RawSource(String name, VideoMode mode) {
027    super(
028        CameraServerJNI.createRawSource(
029            name, mode.pixelFormat.getValue(), mode.width, mode.height, mode.fps));
030  }
031
032  /**
033   * Create a raw frame source.
034   *
035   * @param name Source name (arbitrary unique identifier)
036   * @param pixelFormat Pixel format
037   * @param width width
038   * @param height height
039   * @param fps fps
040   */
041  public RawSource(String name, PixelFormat pixelFormat, int width, int height, int fps) {
042    super(CameraServerJNI.createRawSource(name, pixelFormat.getValue(), width, height, fps));
043  }
044
045  /**
046   * Put a raw image and notify sinks.
047   *
048   * @param image raw frame image
049   */
050  public void putFrame(RawFrame image) {
051    CameraServerJNI.putRawSourceFrame(m_handle, image.getNativeObj());
052  }
053
054  /**
055   * Put a raw image and notify sinks.
056   *
057   * @param data raw frame native data pointer
058   * @param size total size in bytes
059   * @param width frame width
060   * @param height frame height
061   * @param stride size of each row in bytes
062   * @param pixelFormat pixel format
063   */
064  protected void putFrame(
065      long data, int size, int width, int height, int stride, PixelFormat pixelFormat) {
066    CameraServerJNI.putRawSourceFrameData(
067        m_handle, data, size, width, height, stride, pixelFormat.getValue());
068  }
069
070  /**
071   * Put a raw image and notify sinks.
072   *
073   * @param data raw frame native ByteBuffer
074   * @param width frame width
075   * @param height frame height
076   * @param stride size of each row in bytes
077   * @param pixelFormat pixel format
078   */
079  public void putFrame(
080      ByteBuffer data, int width, int height, int stride, PixelFormat pixelFormat) {
081    if (!data.isDirect()) {
082      throw new UnsupportedOperationException("ByteBuffer must be direct");
083    }
084    CameraServerJNI.putRawSourceFrameBB(
085        m_handle, data, data.limit(), width, height, stride, pixelFormat.getValue());
086  }
087}