James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | package edu.wpi.cscore; |
| 9 | |
| 10 | import org.opencv.core.Mat; |
| 11 | |
| 12 | import edu.wpi.cscore.VideoMode.PixelFormat; |
| 13 | |
| 14 | public class RawCVMatSource extends ImageSource { |
| 15 | /** |
| 16 | * Create an OpenCV source. |
| 17 | * |
| 18 | * @param name Source name (arbitrary unique identifier) |
| 19 | * @param mode Video mode being generated |
| 20 | */ |
| 21 | public RawCVMatSource(String name, VideoMode mode) { |
| 22 | super(CameraServerJNI.createRawSource(name, |
| 23 | mode.pixelFormat.getValue(), |
| 24 | mode.width, |
| 25 | mode.height, |
| 26 | mode.fps)); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Create an OpenCV source. |
| 31 | * |
| 32 | * @param name Source name (arbitrary unique identifier) |
| 33 | * @param pixelFormat Pixel format |
| 34 | * @param width width |
| 35 | * @param height height |
| 36 | * @param fps fps |
| 37 | */ |
| 38 | public RawCVMatSource(String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) { |
| 39 | super(CameraServerJNI.createRawSource(name, pixelFormat.getValue(), width, height, fps)); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Put an OpenCV image and notify sinks. |
| 44 | * |
| 45 | * <p>Only 8-bit single-channel or 3-channel (with BGR channel order) images |
| 46 | * are supported. If the format, depth or channel order is different, use |
| 47 | * Mat.convertTo() and/or cvtColor() to convert it first. |
| 48 | * |
| 49 | * @param image OpenCV image |
| 50 | */ |
| 51 | public void putFrame(Mat image) { |
| 52 | int channels = image.channels(); |
| 53 | if (channels != 1 && channels != 3) { |
| 54 | throw new VideoException("Unsupported Image Type"); |
| 55 | } |
| 56 | int imgType = channels == 1 ? PixelFormat.kGray.getValue() : PixelFormat.kBGR.getValue(); |
| 57 | CameraServerJNI.putRawSourceFrame(m_handle, image.dataAddr(), image.width(), image.height(), imgType, (int)image.total() * channels); |
| 58 | } |
| 59 | } |