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