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.util;
006
007import java.nio.ByteBuffer;
008
009/**
010 * Class for storing raw frame data between image read call.
011 *
012 * <p>Data is reused for each frame read, rather then reallocating every frame.
013 */
014public class RawFrame implements AutoCloseable {
015  private final long m_framePtr;
016  private ByteBuffer m_dataByteBuffer;
017  private long m_dataPtr;
018  private int m_totalData;
019  private int m_width;
020  private int m_height;
021  private int m_pixelFormat;
022
023  /** Construct a new RawFrame. */
024  public RawFrame() {
025    m_framePtr = WPIUtilJNI.allocateRawFrame();
026  }
027
028  /**
029   * Close the RawFrame, releasing native resources. Any images currently using the data will be
030   * invalidated.
031   */
032  @Override
033  public void close() {
034    WPIUtilJNI.freeRawFrame(m_framePtr);
035  }
036
037  /**
038   * Called from JNI to set data in class.
039   *
040   * @param dataByteBuffer A ByteBuffer pointing to the frame data.
041   * @param dataPtr A long (a char* in native code) pointing to the frame data.
042   * @param totalData The total length of the data stored in the frame.
043   * @param width The width of the frame.
044   * @param height The height of the frame.
045   * @param pixelFormat The PixelFormat of the frame.
046   */
047  public void setData(
048      ByteBuffer dataByteBuffer,
049      long dataPtr,
050      int totalData,
051      int width,
052      int height,
053      int pixelFormat) {
054    m_dataByteBuffer = dataByteBuffer;
055    m_dataPtr = dataPtr;
056    m_totalData = totalData;
057    m_width = width;
058    m_height = height;
059    m_pixelFormat = pixelFormat;
060  }
061
062  /**
063   * Get the pointer to native representation of this frame.
064   *
065   * @return The pointer to native representation of this frame.
066   */
067  public long getFramePtr() {
068    return m_framePtr;
069  }
070
071  /**
072   * Get a ByteBuffer pointing to the frame data. This ByteBuffer is backed by the frame directly.
073   * Its lifetime is controlled by the frame. If a new frame gets read, it will overwrite the
074   * current one.
075   *
076   * @return A ByteBuffer pointing to the frame data.
077   */
078  public ByteBuffer getDataByteBuffer() {
079    return m_dataByteBuffer;
080  }
081
082  /**
083   * Get a long (is a char* in native code) pointing to the frame data. This pointer is backed by
084   * the frame directly. Its lifetime is controlled by the frame. If a new frame gets read, it will
085   * overwrite the current one.
086   *
087   * @return A long pointing to the frame data.
088   */
089  public long getDataPtr() {
090    return m_dataPtr;
091  }
092
093  /**
094   * Get the total length of the data stored in the frame.
095   *
096   * @return The total length of the data stored in the frame.
097   */
098  public int getTotalData() {
099    return m_totalData;
100  }
101
102  /**
103   * Get the width of the frame.
104   *
105   * @return The width of the frame.
106   */
107  public int getWidth() {
108    return m_width;
109  }
110
111  /**
112   * Set the width of the frame.
113   *
114   * @param width The width of the frame.
115   */
116  public void setWidth(int width) {
117    this.m_width = width;
118  }
119
120  /**
121   * Get the height of the frame.
122   *
123   * @return The height of the frame.
124   */
125  public int getHeight() {
126    return m_height;
127  }
128
129  /**
130   * Set the height of the frame.
131   *
132   * @param height The height of the frame.
133   */
134  public void setHeight(int height) {
135    this.m_height = height;
136  }
137
138  /**
139   * Get the PixelFormat of the frame.
140   *
141   * @return The PixelFormat of the frame.
142   */
143  public int getPixelFormat() {
144    return m_pixelFormat;
145  }
146
147  /**
148   * Set the PixelFormat of the frame.
149   *
150   * @param pixelFormat The PixelFormat of the frame.
151   */
152  public void setPixelFormat(int pixelFormat) {
153    this.m_pixelFormat = pixelFormat;
154  }
155}