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