[wpiutil,cscore] Move VideoMode.PixelFormat to wpiutil (#6097)

This tracks the RawFrame move.
This commit is contained in:
Peter Johnson
2023-12-26 14:47:17 -08:00
committed by GitHub
parent ab78b930e9
commit 55508706ff
8 changed files with 70 additions and 39 deletions

View File

@@ -14,7 +14,6 @@ import edu.wpi.first.cscore.VideoEvent;
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.StringArrayPublisher;
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 final class CameraServer {
*/
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 final class CameraServer {
* @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;
}

View File

@@ -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;
/**

View File

@@ -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 class CvSource extends ImageSource {
* @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));
}

View File

@@ -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 class VideoMode {
* @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;

View File

@@ -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 class VideoSource implements AutoCloseable {
* @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 class VideoSource implements AutoCloseable {
* @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());
}

View File

@@ -7,6 +7,7 @@ package edu.wpi.first.cscore.raw;
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 class RawSource extends ImageSource {
* @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 @@ public class RawSource extends ImageSource {
* @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);
}

View File

@@ -7,13 +7,14 @@ package edu.wpi.first.cscore;
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);

View File

@@ -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];
}
}