diff --git a/cameraserver/src/main/java/edu/wpi/first/cameraserver/CameraServer.java b/cameraserver/src/main/java/edu/wpi/first/cameraserver/CameraServer.java index 397cd20809e..24f2ba28d01 100644 --- a/cameraserver/src/main/java/edu/wpi/first/cameraserver/CameraServer.java +++ b/cameraserver/src/main/java/edu/wpi/first/cameraserver/CameraServer.java @@ -14,7 +14,6 @@ import edu.wpi.first.cscore.VideoException; import edu.wpi.first.cscore.VideoListener; import edu.wpi.first.cscore.VideoMode; -import edu.wpi.first.cscore.VideoMode.PixelFormat; import edu.wpi.first.cscore.VideoSink; import edu.wpi.first.cscore.VideoSource; import edu.wpi.first.networktables.BooleanEntry; @@ -27,6 +26,7 @@ import edu.wpi.first.networktables.StringArrayTopic; import edu.wpi.first.networktables.StringEntry; import edu.wpi.first.networktables.StringPublisher; +import edu.wpi.first.util.PixelFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -686,7 +686,7 @@ public static AxisCamera addAxisCamera(String name, String[] hosts) { */ public static MjpegServer addSwitchedCamera(String name) { // create a dummy CvSource - CvSource source = new CvSource(name, VideoMode.PixelFormat.kMJPEG, 160, 120, 30); + CvSource source = new CvSource(name, PixelFormat.kMJPEG, 160, 120, 30); MjpegServer server = startAutomaticCapture(source); synchronized (CameraServer.class) { m_fixedSources.put(server.getHandle(), source.getHandle()); @@ -801,7 +801,7 @@ public static CvSink getVideo(String name) { * @return OpenCV source for the MJPEG stream */ public static CvSource putVideo(String name, int width, int height) { - CvSource source = new CvSource(name, VideoMode.PixelFormat.kMJPEG, width, height, 30); + CvSource source = new CvSource(name, PixelFormat.kMJPEG, width, height, 30); startAutomaticCapture(source); return source; } diff --git a/cscore/src/main/java/edu/wpi/first/cscore/CvSink.java b/cscore/src/main/java/edu/wpi/first/cscore/CvSink.java index 75d9e1cbc1f..af390e5967f 100644 --- a/cscore/src/main/java/edu/wpi/first/cscore/CvSink.java +++ b/cscore/src/main/java/edu/wpi/first/cscore/CvSink.java @@ -4,7 +4,7 @@ package edu.wpi.first.cscore; -import edu.wpi.first.cscore.VideoMode.PixelFormat; +import edu.wpi.first.util.PixelFormat; import org.opencv.core.Mat; /** diff --git a/cscore/src/main/java/edu/wpi/first/cscore/CvSource.java b/cscore/src/main/java/edu/wpi/first/cscore/CvSource.java index 3934a09c0b4..4c29ffc482e 100644 --- a/cscore/src/main/java/edu/wpi/first/cscore/CvSource.java +++ b/cscore/src/main/java/edu/wpi/first/cscore/CvSource.java @@ -4,6 +4,7 @@ package edu.wpi.first.cscore; +import edu.wpi.first.util.PixelFormat; import org.opencv.core.Mat; /** @@ -32,7 +33,7 @@ public CvSource(String name, VideoMode mode) { * @param height height * @param fps fps */ - public CvSource(String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) { + public CvSource(String name, PixelFormat pixelFormat, int width, int height, int fps) { super(CameraServerCvJNI.createCvSource(name, pixelFormat.getValue(), width, height, fps)); } diff --git a/cscore/src/main/java/edu/wpi/first/cscore/VideoMode.java b/cscore/src/main/java/edu/wpi/first/cscore/VideoMode.java index b818c7e4665..712f695bb68 100644 --- a/cscore/src/main/java/edu/wpi/first/cscore/VideoMode.java +++ b/cscore/src/main/java/edu/wpi/first/cscore/VideoMode.java @@ -4,38 +4,12 @@ package edu.wpi.first.cscore; +import edu.wpi.first.util.PixelFormat; import java.util.Objects; /** Video mode. */ @SuppressWarnings("MemberName") public class VideoMode { - public enum PixelFormat { - kUnknown(0), - kMJPEG(1), - kYUYV(2), - kRGB565(3), - kBGR(4), - kGray(5), - kY16(6), - kUYVY(7); - - private final int value; - - PixelFormat(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - } - - private static final PixelFormat[] m_pixelFormatValues = PixelFormat.values(); - - public static PixelFormat getPixelFormatFromInt(int pixelFormat) { - return m_pixelFormatValues[pixelFormat]; - } - /** * Create a new video mode. * @@ -45,7 +19,7 @@ public static PixelFormat getPixelFormatFromInt(int pixelFormat) { * @param fps The camera's frames per second. */ public VideoMode(int pixelFormat, int width, int height, int fps) { - this.pixelFormat = getPixelFormatFromInt(pixelFormat); + this.pixelFormat = PixelFormat.getFromInt(pixelFormat); this.width = width; this.height = height; this.fps = fps; diff --git a/cscore/src/main/java/edu/wpi/first/cscore/VideoSource.java b/cscore/src/main/java/edu/wpi/first/cscore/VideoSource.java index 0ae5adddc66..32c784998a9 100644 --- a/cscore/src/main/java/edu/wpi/first/cscore/VideoSource.java +++ b/cscore/src/main/java/edu/wpi/first/cscore/VideoSource.java @@ -4,6 +4,8 @@ package edu.wpi.first.cscore; +import edu.wpi.first.util.PixelFormat; + /** * A source for video that provides a sequence of frames. Each frame may consist of multiple images * (e.g. from a stereo or depth camera); these are called channels. @@ -235,7 +237,7 @@ public boolean setVideoMode(VideoMode mode) { * @param fps desired FPS * @return True if set successfully */ - public boolean setVideoMode(VideoMode.PixelFormat pixelFormat, int width, int height, int fps) { + public boolean setVideoMode(PixelFormat pixelFormat, int width, int height, int fps) { return CameraServerJNI.setSourceVideoMode(m_handle, pixelFormat.getValue(), width, height, fps); } @@ -245,7 +247,7 @@ public boolean setVideoMode(VideoMode.PixelFormat pixelFormat, int width, int he * @param pixelFormat desired pixel format * @return True if set successfully */ - public boolean setPixelFormat(VideoMode.PixelFormat pixelFormat) { + public boolean setPixelFormat(PixelFormat pixelFormat) { return CameraServerJNI.setSourcePixelFormat(m_handle, pixelFormat.getValue()); } diff --git a/cscore/src/main/java/edu/wpi/first/cscore/raw/RawSource.java b/cscore/src/main/java/edu/wpi/first/cscore/raw/RawSource.java index 900a6705ecb..252f7a6efb1 100644 --- a/cscore/src/main/java/edu/wpi/first/cscore/raw/RawSource.java +++ b/cscore/src/main/java/edu/wpi/first/cscore/raw/RawSource.java @@ -7,6 +7,7 @@ import edu.wpi.first.cscore.CameraServerJNI; import edu.wpi.first.cscore.ImageSource; import edu.wpi.first.cscore.VideoMode; +import edu.wpi.first.util.PixelFormat; import edu.wpi.first.util.RawFrame; /** @@ -36,7 +37,7 @@ public RawSource(String name, VideoMode mode) { * @param height height * @param fps fps */ - public RawSource(String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) { + public RawSource(String name, PixelFormat pixelFormat, int width, int height, int fps) { super(CameraServerJNI.createRawSource(name, pixelFormat.getValue(), width, height, fps)); } @@ -72,7 +73,7 @@ protected void putFrame(long data, int width, int height, int pixelFormat, int t * @param totalData length of data in total */ protected void putFrame( - long data, int width, int height, VideoMode.PixelFormat pixelFormat, int totalData) { + long data, int width, int height, PixelFormat pixelFormat, int totalData) { CameraServerJNI.putRawSourceFrame( m_handle, data, width, height, pixelFormat.getValue(), totalData); } diff --git a/cscore/src/test/java/edu/wpi/first/cscore/VideoModeTest.java b/cscore/src/test/java/edu/wpi/first/cscore/VideoModeTest.java index 738888d7fe8..6108c5ca1cb 100644 --- a/cscore/src/test/java/edu/wpi/first/cscore/VideoModeTest.java +++ b/cscore/src/test/java/edu/wpi/first/cscore/VideoModeTest.java @@ -7,13 +7,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import edu.wpi.first.util.PixelFormat; import org.junit.jupiter.api.Test; class VideoModeTest { @Test void equalityTest() { - VideoMode a = new VideoMode(VideoMode.PixelFormat.kMJPEG, 1920, 1080, 30); - VideoMode b = new VideoMode(VideoMode.PixelFormat.kMJPEG, 1920, 1080, 30); + VideoMode a = new VideoMode(PixelFormat.kMJPEG, 1920, 1080, 30); + VideoMode b = new VideoMode(PixelFormat.kMJPEG, 1920, 1080, 30); assertEquals(a, b); assertNotEquals(a, null); diff --git a/wpiutil/src/main/java/edu/wpi/first/util/PixelFormat.java b/wpiutil/src/main/java/edu/wpi/first/util/PixelFormat.java new file mode 100644 index 00000000000..809bfd0361b --- /dev/null +++ b/wpiutil/src/main/java/edu/wpi/first/util/PixelFormat.java @@ -0,0 +1,52 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +package edu.wpi.first.util; + +/** Image pixel format. */ +public enum PixelFormat { + /** Unknown format. */ + kUnknown(0), + /** Motion-JPEG (compressed image data). */ + kMJPEG(1), + /** YUY 4:2:2, 16 bpp. */ + kYUYV(2), + /** RGB 5-6-5, 16 bpp. */ + kRGB565(3), + /** BGR 8-8-8, 24 bpp. */ + kBGR(4), + /** Grayscale, 8 bpp. */ + kGray(5), + /** Grayscale, 16 bpp. */ + kY16(6), + /** YUV 4:2:2, 16 bpp. */ + kUYVY(7); + + private final int value; + + PixelFormat(int value) { + this.value = value; + } + + /** + * Gets the integer value of the pixel format. + * + * @return Integer value + */ + public int getValue() { + return value; + } + + private static final PixelFormat[] s_values = values(); + + /** + * Gets a PixelFormat enum value from its integer value. + * + * @param pixelFormat integer value + * @return Enum value + */ + public static PixelFormat getFromInt(int pixelFormat) { + return s_values[pixelFormat]; + } +}