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.ImageSink;
009import edu.wpi.first.util.RawFrame;
010
011/**
012 * A sink for user code to accept video frames as raw bytes.
013 *
014 * <p>This is a complex API, most cases should use CvSink.
015 */
016public class RawSink extends ImageSink {
017  /**
018   * Create a sink for accepting raw images.
019   *
020   * <p>grabFrame() must be called on the created sink to get each new image.
021   *
022   * @param name Source name (arbitrary unique identifier)
023   */
024  public RawSink(String name) {
025    super(CameraServerJNI.createRawSink(name));
026  }
027
028  /**
029   * Wait for the next frame and get the image. Times out (returning 0) after 0.225 seconds. The
030   * provided image will have three 8-bit channels stored in BGR order.
031   *
032   * @param frame The frame object in which to store the image.
033   * @return Frame time, or 0 on error (call getError() to obtain the error message); the frame time
034   *     is in the same time base as wpi::Now(), and is in 1 us increments.
035   */
036  public long grabFrame(RawFrame frame) {
037    return grabFrame(frame, 0.225);
038  }
039
040  /**
041   * Wait for the next frame and get the image. Times out (returning 0) after timeout seconds. The
042   * provided image will have three 8-bit channels stored in BGR order.
043   *
044   * @param frame The frame object in which to store the image.
045   * @param timeout The frame timeout in seconds.
046   * @return Frame time, or 0 on error (call getError() to obtain the error message); the frame time
047   *     is in the same time base as wpi::Now(), and is in 1 us increments.
048   */
049  public long grabFrame(RawFrame frame, double timeout) {
050    return CameraServerJNI.grabRawSinkFrameTimeout(m_handle, frame, frame.getNativeObj(), timeout);
051  }
052
053  /**
054   * Wait for the next frame and get the image. May block forever. The provided image will have
055   * three 8-bit channels stored in BGR order.
056   *
057   * @param frame The frame object in which to store the image.
058   * @return Frame time, or 0 on error (call getError() to obtain the error message); the frame time
059   *     is in the same time base as wpi::Now(), and is in 1 us increments.
060   */
061  public long grabFrameNoTimeout(RawFrame frame) {
062    return CameraServerJNI.grabRawSinkFrame(m_handle, frame, frame.getNativeObj());
063  }
064}